Reputation: 95
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.
Upvotes: 0
Views: 6585
Reputation: 326
The error messages says your (single) quotation marks do not pair.
[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]문자열 '0???뜝詵? *낷놑 '의 따옴표가 짝이 맞지 않습니다. (105) (SQLExecDirectW); [42000] [Microsoft][ODBC SQL Server Driver][SQL Server]'0???뜝詵? *낷놑 ' 근처의 구문이 잘못되었습니다. (102)
Your sssd must have at least one quotation mark in it, corrupting your directly built query sentence.
Upvotes: 0
Reputation: 1367
it looks like you aren't converting to the correct datatypes, when I ran the following on 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
and it should answer your question
Upvotes: 1