pyNihat
pyNihat

Reputation: 31

Inserting Long Binary data into Access db using pyodbc

I can insert text and integer data to MS Access db (.mdb) by using pyodbc package. But now i want to insert Large Binary objects. I have a table that consists ID(COUNTER type), Name(VARCHAR type), File (LONGBINARY type), Author(VARCHAR type) columns. I use that code to insert some text and int data:

cursor.execute("""INSERT INTO table(ID, Name) VALUES(1,'book')""")

After that i used that code but always getting error.

with open('c:/tree.jpg', 'rb') as file:
      binData = file.read()

SQL = """INSERT INTO table VALUES(2,'threePicture', %s, 'Mike')""" %(binData)

cursor.execute(SQL)

The error is: ProgrammingError: ('42000', "[42000])

Upvotes: 1

Views: 929

Answers (1)

pyNihat
pyNihat

Reputation: 31

I found the solution using ? ? ? characters...

cursor.execute("insert into table values(?, ?, ?, ?)", 2, 'treePicture', pyodbc.Binary(binData), 'Mike')

Use ? chars for values in expression.

Upvotes: 1

Related Questions