python 3.x将bytearray写入sqlite3 blob字段

时间:2015-08-11 12:11:07

标签: python python-3.x sqlite

我有一些信息要将blob字段保存到sqlite DB中。

例如:

out = [83, 105, 108, 105, 99, 111, 110, 32, 86, 97, 108, 108, 101, 121, 32, 40, 84, 86, 32, 115, 101, 114, 105, 101, 115, 41, 13, 10, 70, 114, 111, 109, 32, 87, 105, 107, 105, 112, 101, 100, 105, 97, 44, 32, 116, 104, 101, 32, 102, 114, 101, 101, 32, 101, 110, 99, 121, 99, 108, 111, 112, 101, 100, 105, 97, 13, 10, 83, 105, 108, 105, 99, 111, 110, 32, 86, 97, 108, 108, 101, 121, 32, 105, 115, 32, 97, 110, 32, 65, 109, 101, 114, 105, 99, 97, 110, 32, 116, 101, 108, 101, 118, 105, 115, 105, 111, 110, 32, 99, 111, 109, 101, 100, 121, 32, 115, 101, 114, 105, 101, 115, 32, 99, 114, 101, 97, 116, 101]

blob = bytearray(out)
print(blob)

在这些情况下输出为:

bytearray(b'Silicon Valley (TV series)\r\nFrom Wikipedia, the free encyclopedia\r\nSilicon Valley is an American television comedy series create')

(我从维基百科复制了它)

我需要使用此信息插入新记录。我有这段代码:

import sqlite3

out = [83, 105, 108, 105, 99, 111, 110, 32, 86, 97, 108, 108, 101, 121, 32, 40, 84, 86, 32, 115, 101, 114, 105, 101, 115, 41,
13, 10, 70, 114, 111, 109, 32, 87, 105, 107, 105, 112, 101, 100, 105, 97, 44, 32, 116, 104, 101, 32, 102, 114, 101, 101,
 32, 101, 110, 99, 121, 99, 108, 111, 112, 101, 100, 105, 97, 13, 10, 83, 105, 108, 105, 99, 111, 110, 32, 86, 97, 108,
108, 101, 121, 32, 105, 115, 32, 97, 110, 32, 65, 109, 101, 114, 105, 99, 97, 110, 32, 116, 101, 108, 101, 118, 105, 115
, 105, 111, 110, 32, 99, 111, 109, 101, 100, 121, 32, 115, 101, 114, 105, 101, 115, 32, 99, 114, 101, 97, 116, 101]

blob = bytearray(out)
print(blob)

Con = sqlite3.connect('info.db')
Cur = Con.cursor()

Cur.execute("create table t_info (Primary_Key INTEGER PRIMARY KEY ASC, Info_Value BLOB )")

try:
    Cur.execute("insert into t_info (Primary_Key, Info_Value) values (1, X'" + blob.tostring() + "')")
except:
    print('error')

你能告诉我我的错误在哪里吗?

2 个答案:

答案 0 :(得分:2)

您的毯子try:..except掩盖了bytearray个对象没有.tostring()方法的事实。即使有这样的方法,将字节转换为字符串也是错误的方法。

不要使用字符串插值。使用SQL参数:

Cur.execute("insert into t_info (Primary_Key, Info_Value) values (1, ?)", (blob,))

这使数据库库可以为您处理类型,同时保护您免受SQL注入攻击。

查询中的?是一个SQL参数;第二个参数中的每个值用于填充参数。

演示:

>>> Cur.execute("insert into t_info (Primary_Key, Info_Value) values (1, ?)", (blob,))
<sqlite3.Cursor object at 0x10a0ca810>
>>> Con.commit()
>>> Cur = Con.cursor()
>>> Cur.execute('select * from t_info')
<sqlite3.Cursor object at 0x10a0caab0>
>>> list(Cur)
[(1, b'Silicon Valley (TV series)\r\nFrom Wikipedia, the free encyclopedia\r\nSilicon Valley is an American television comedy series create')]

请注意,在查询时,该列将作为bytes对象返回。

答案 1 :(得分:0)

问题是void pushFront(int value); int popFront(); void pushBack(int value); int popBack(); void clearAll(); 没有bytearrays方法,所以你的代码在try / except中出错了,但你没有看到任何输出,因为你有一个毯子,除非你在异常时什么都不做发生这样代码运行但错误无声地传递。

您可以使用Martijn的答案正确插入,但一般情况下,如果您使用tostring捕获您期望的异常并非每个例外,如果您发生了try/except错误,则显而易见:

tostring

哪会给你:

try:
    Cur.execute("insert into t_info (Primary_Key, Info_Value) values (1, X'" + blob.tostring() + "')")
except sqlite3.Error as e:
    print(e)