【问题标题】:Cannot insert NULL into column 'Type'无法将 NULL 插入“类型”列
【发布时间】: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 表必须有一个名为 TypeNOT NULL 列。由于您没有在插入中包含Type 列,因此插入的值默认为NULL,因此会出现错误。
  • 也许你有另一个数据库或架构有一个Notes 表,但beltoutlet.dbo.Notes 肯定有一个Type 列。
  • 要详细说明@AaronDietz 所说的内容,当您执行查询时,默认的 catalog(数据库)是“beltoutlet”,默认的 schema 是“dbo”。如果这些值是正确的,那么问题的最可能原因是您没有连接到您认为自己的 SQL Server instance(即,一个确实有一个 beltoutlet.dbo.NotesType 非空列)。
  • 谢谢大家。我确实有两个数据库,但我在脚本前面用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


【解决方案1】:

我同意 Aaron Dietz 的 cmets 的观点,即被引用的表(不一定是您要引用的表)有一个 Type 列。为了纠正这种情况,我相信您有两种选择:

1) 使用数据库和模式名称指定确切的表

cursor.execute("insert into [databaseName].[SchemaName].[Notes](Notes, NumericKey) values (?, ?)",Tracking, OrderNumber)

2) (不推荐)通过将其设置为 false 值(如空字符串)来强制插入

cursor.execute("insert into Notes(Notes, NumericKey, [Type]) values (?, ?, ?)",Tracking, OrderNumber, '')

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2020-06-26
    • 1970-01-01
    • 1970-01-01
    • 2013-02-27
    • 2021-10-31
    • 2012-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多