简体   繁体   English

Python pyODBC:问题插入到带有标识列的 sql server 表中

[英]Python pyODBC : Issue inserting into a sql server table with an identity column

An INSERT statement that was created with Python gives me an error when I execute and commit it.使用 Python 创建的 INSERT 语句在我执行和提交时给我一个错误。 I have taken a copy of the statement from Python and run it myself in SQL SERVER and it works fine there.我从 Python 中获取了该语句的副本,并在 SQL SERVER 中自己运行它,它在那里运行良好。 The table I am trying to insert into has an identity column.我试图插入的表有一个标识列。 When Python trys to execute it will give me an error saying what is below when I exclude the identity column in the statement当 Python 尝试执行时,当我在语句中排除标识列时,它会给我一个错误,说明下面的内容

Table looks like this MY_TABLE ( ID INT IDENTITY(1,1) NOT NULL, A INT, B INT)表看起来像这样 MY_TABLE ( ID INT IDENTITY(1,1) NOT NULL, A INT, B INT)

INSERT INTO MY_TABLE (A, B) VALUES(VALUE_A, VALUE_B);

"('23000', "[23000] [Microsoft][ODBC SQL Server Driver][SQL Server]Cannot insert the value NULL into column 'MY_IDENTITY_COLUMN', table 'MY_TABLE'; column does not allow nulls. INSERT fails. (515) (SQLExecDirectW); [23000] [Microsoft][ODBC SQL Server Driver][SQL Server]The statement has been terminated. (3621)")" "('23000', "[23000] [Microsoft][ODBC SQL Server 驱动程序][SQL Server]无法将值 NULL 插入列 'MY_IDENTITY_COLUMN',表 'MY_TABLE';列不允许空值。插入失败。(515 ) (SQLExecDirectW); [23000] [Microsoft][ODBC SQL Server Driver][SQL Server]语句已终止。(3621)")"

But when I try to include the value for the Identity column (I don't want to do this) I get the following error which makes sense as it's an identity column that we let the table auto-increment但是,当我尝试包含 Identity 列的值(我不想这样做)时,我收到以下错误,这是有道理的,因为它是我们让表自动递增的标识列

"Cannot insert explicit value for identity column in table 'MY_TABLE' when IDENTITY_INSERT is set to OFF." “当 IDENTITY_INSERT 设置为 OFF 时,无法为表 'MY_TABLE' 中的标识列插入显式值。”

When I run the query in SQL SERVER the table fills the value for the Identity Column itself and auto-increments but for some reason when I run the statement in Python it does not do this and tries to pass a NULL当我在 SQL SERVER 中运行查询时,该表会填充标识列本身的值并自动递增,但是由于某种原因,当我在 Python 中运行该语句时,它不会这样做并尝试传递 NULL

SQL SERVER version: 10.50.6560.0 SQL SERVER 版本:10.50.6560.0

A little hard to tell without your code, but here is an example.没有你的代码有点难以判断,但这里有一个例子。

In database create a test table在数据库中创建一个测试表

CREATE TABLE dbo.test(  
ID INT IDENTITY NOT NULL PRIMARY KEY,   
Name VARCHAR(40) NOT NULL  
);  

Then in Python specify the columns you are inserting into然后在 Python 中指定要插入的列

import pyodbc

warecn = pyodbc.connect("Your Connection Stuff")

Inscursor = warecn.cursor()

Inscursor.execute("Insert into dbo.test(name) values ('This'), ('is'), ('a'), ('test')")


Inscursor.commit()
Inscursor.close()
del Inscursor
warecn.close()

Recognising it's a little late but... I had the same problem recently using some old program and ODBC.认识到它有点晚了但是......我最近使用一些旧程序和 ODBC 遇到了同样的问题。 The solution was to create a View in SQL Server with only the columns required (ie A and B in your case) and then insert into that View.解决方案是在 SQL Server 中创建一个仅包含所需列(即 A 和 B 在您的情况下)的视图,然后插入到该视图中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM