简体   繁体   English

使用Python pyodbc将数据插入MS SQL Server时出错[错误42000]

[英]Error Inserting data into MS SQL Server Using Python pyodbc[Error 42000]

fo = open("C:\Program Files\NPKI\yessign\User\SignCert.der","rb")
sssd = fo.read()
std = "SignCert.der"
cursor.execute("SELECT * FROM NPKIInf WHERE nSeqNum=5")

cursor.execute("insert into NPKIInf(nSeqNum, nFileName, nBinary) values ('7', '" + std + "', '" + sssd +"')")
cnxn.commit()

Above is the code and the error msg is down below: 上面是代码,下面是错误消息:

C:\Python27>python import.py
Traceback (most recent call last):
  File "import.py", line 12, in <module>
    cursor.execute("insert into NPKIInf(nSeqNum,nFileName,nBinary) values('7', '" + std + "', '" + sssd +"')")
pyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][
SQL Server]\xb9\xae\xc0\xda\xbf\xad '0???\x8d\xa0\x03\x02\x01\x02\x02\x04\x14\xe
0\xd2?\r\x06\t*\x86H\x86\xf7\r\x01\x01\x0b\x05'\xc0\xc7 \xb5\xfb\xbf\xc8\xc7\xa5
\xb0\xa1 \xc2\xa6\xc0\xcc \xb8\xc2\xc1\xf6 \xbe\xca\xbd\xc0\xb4\xcf\xb4\xd9. (10
5) (SQLExecDirectW); [42000] [Microsoft][ODBC SQL Server Driver][SQL Server]'0??
?\x8d\xa0\x03\x02\x01\x02\x02\x04\x14\xe0\xd2?\r\x06\t*\x86H\x86\xf7\r\x01\x01\x
0b\x05' \xb1\xd9\xc3\xb3\xc0\xc7 \xb1\xb8\xb9\xae\xc0\xcc \xc0\xdf\xb8\xf8\xb5\x
c7\xbe\xfa\xbd\xc0\xb4\xcf\xb4\xd9. (102)")

Connection to DB is Fine, I also tried just inserting hard data like 与数据库的连接很好,我也尝试过仅插入硬数据,例如

cursor.execute("insert into NPKIInf(nSeqNum,nFileName,nBinary) values('7','test','test'")

Still doesn't work. 仍然不起作用。

this is the information of DB Table: 这是数据库表的信息:

 CREATE TABLE [dbo].[NPKIInf](
           [nSeqNum] [smallint] NOT NULL,
           [nFileName] [nchar](50) NULL,
           [nBinary] [varbinary](max) NULL,
           [nHspName] [nchar](50) NULL,
CONSTRAINT [PK_NPKIInf] PRIMARY KEY CLUSTERED
(
           [nSeqNum] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

What am I doing wrong? 我究竟做错了什么? Thanks in advance. 提前致谢。

it looks like you aren't converting to the correct datatypes, when I ran the following on SQL Server: 当我在SQL Server上运行以下命令时,您似乎没有转换为正确的数据类型:

insert into NPKIInf(nSeqNum,nFileName,nBinary) values('8','test', 'test')

it failed, when I ran: 当我跑的时候失败了:

insert into NPKIInf(nSeqNum,nFileName,nBinary) values('8','test', cast('test' as varbinary))

as far as you're actual query goes, take a looke at: Inserting pyodbc.Binary data (BLOB) into SQL Server image column 就您实际的查询而言,请看一下: 将pyodbc.Binary数据(BLOB)插入SQL Server映像列

and it should answer your question 它应该回答你的问题

The error messages says your (single) quotation marks do not pair. 错误消息表明您的(单引号)不匹配。

[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]문자열 '0???뜝詵? [42000] [Microsoft] [ODBC SQL Server驱动程序] [SQL Server]문자열'0 ???뜝诜'? *낷놑 ?'의 따옴표가 짝이 맞지 않습니다. *낷놑?'의의가따옴표이맞지맞다。 (105) (SQLExecDirectW); (105)(SQLExecDirectW); [42000] [Microsoft][ODBC SQL Server Driver][SQL Server]'0???뜝詵? [42000] [Microsoft] [ODBC SQL Server驱动程序] [SQL Server]'0 ???뜝诜? *낷놑 ?' *낷놑?' 근처의 구문이 잘못되었습니다. 。이잘못되었습니다。 (102) (102)

Your sssd must have at least one quotation mark in it, corrupting your directly built query sentence. 您的sssd必须至少包含一个引号,从而破坏您直接构建的查询语句。

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

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