The first argument to execute must be a string or unicode query

后端 未结 1 882
青春惊慌失措
青春惊慌失措 2021-01-13 13:33

I am trying to upload a blob data to ms-sql db, using pyodbc. And I get \"the first argument to execute must be a string or unicode query\" error.

T

相关标签:
1条回答
  • 2021-01-13 14:24

    Use parameterized insert:

    file = pyodbc.Binary(open("some_pdf_file.pdf", "r").read())
    sql = "insert into BlobDataForPDF(ObjectID, FileData, Extension) values (?, ?, ?)"
    cur.execute(sql, ('1', file, '.PDF'))
    cur.commit()
    

    The current code is attempting to concatenate binary data with your insert string. Using parameters isolates your SQL string from the inserted values, protecting against SQL injection and is more efficient if you execute the insert multiple times with different values. Sample usage here.

    0 讨论(0)
提交回复
热议问题