【发布时间】:2018-01-09 16:58:12
【问题描述】:
我正在尝试将包含两条信息的行添加到 Notes 表中。我正在使用 python 执行以下INSERT 语句。
cursor.execute("insert into Notes(Notes, NumericKey) values (?, ?)",Tracking, OrderNumber)
我得到它的错误
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
IntegrityError: ('23000', "[23000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Cannot insert the value NULL into column 'Type', table 'beltoutlet.dbo.Notes'; column does not allow nulls. INSERT fails. (515) (SQLExecDirectW); [01000] [Microsoft][SQL Server Native Client 11.0][SQL Server]The statement has been terminated. (3621)")
我看到很多人有类似的问题,但所有尝试的解决方案都没有帮助。我束手无策,因为 Tracking 和 OrderNumber 都有实际值,而且我没有“类型”列。任何帮助将不胜感激。
【问题讨论】:
-
Notes表必须有一个名为Type的NOT NULL列。由于您没有在插入中包含Type列,因此插入的值默认为NULL,因此会出现错误。 -
也许你有另一个数据库或架构有一个
Notes表,但beltoutlet.dbo.Notes肯定有一个Type列。 -
要详细说明@AaronDietz 所说的内容,当您执行查询时,默认的 catalog(数据库)是“beltoutlet”,默认的 schema 是“dbo”。如果这些值是正确的,那么问题的最可能原因是您没有连接到您认为自己的 SQL Server instance(即,一个确实有一个
beltoutlet.dbo.Notes表Type非空列)。 -
谢谢大家。我确实有两个数据库,但我在脚本前面用
db = pyodbc.connect("DSN=beltoutletsql;UID=beltoutlet;PWD=something")和cursor = db.cursor()指定了它 -
哇哦。是的,有一个类型列。它在关键值之前,我一定是在看它。那很愚蠢。非常感谢你们。我刚刚做了
cursor.execute("insert into Notes(Type, Notes, NumericKey) values (?, ?, ?)", ' ',Tracking, OrderNumber),它终于奏效了。 :D
标签: python sql python-2.7 pyodbc