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))
精彩评论