开发者

How do you escape a binary file for sqlite3 in python?

开发者 https://www.devze.com 2023-03-24 02:54 出处:网络
The python sqlite3 documentation gives this example for inserting parameters into an SQL query. for t in [(\'2006-03-28\', \'BUY\', \'IBM\', 1000, 45.00),

The python sqlite3 documentation gives this example for inserting parameters into an SQL query.

for t in [('2006-03-28', 'BUY', 'IBM', 1000, 45.00),
          ('2006-04-05', 'BUY', 'MSOFT', 1000, 72.00),
 开发者_JS百科         ('2006-04-06', 'SELL', 'IBM', 500, 53.00),
         ]:
    c.execute('insert into stocks values (?,?,?,?,?)', t)

That's fine for text and numbers. What if one of the fields is a binary BLOB, such as a JPEG image? How does one insert a binary file?


Insert binary data just as you would for any other type of field:

contents = open(image_path, "rb").read()
c.execute('insert into images values (?, ?)', (image_path, contents))
0

精彩评论

暂无评论...
验证码 换一张
取 消