在没有 b' 的情况下将字节数据插入 Sqlite
Insert Byte Data to Sqlite without b' preceeding
我正在尝试向我的 sqlite3 数据库中插入一个加密的用户名,但它包括 b'
处理实际值。
这导致我的应用程序出现问题。
我的代码如下:
hashed_username = enc(prompt_account_username, salt)
print('\n\nusername: {}\n\n'.format(hashed_username))
这会打印:
username:
b'sc\x00\x02\x16\x88\x04\xa2\x9d\xb9!\xe0\x9e-5\xb3\x8a\xd8r\xbc\x83\x01\r\x98\xb1\xe3\xb2i=\x16\xc8Y\xbe\xdd\x0f\x8e\x8c\xa2\xb0\xd0R\xf7gx2\x1d\xfb\xfeX\xda\xb9Y\xd6Ls\x88\xaa<\x9c\x12\xf3\xbeq\r\x8d\xcb\xc0\x8adF\x1dl\xc5\xde'
然后插入这个完整的字节值,包括 b'
当我运行:
conn.execute("""
INSERT INTO my_acct
(acct_username)
VALUES (?) """, (v[0], )
)
我怎样才能让它插入值,而不是 b'
,值如下:
sc\x00\x02\x16\x88\x04\xa2\x9d\xb9!\xe0\x9e-5\xb3\x8a\xd8r\xbc\x83\x01\r\x98\xb1\xe3\xb2i=\x16\xc8Y\xbe\xdd\x0f\x8e\x8c\xa2\xb0\xd0R\xf7gx2\x1d\xfb\xfeX\xda\xb9Y\xd6Ls\x88\xaa<\x9c\x12\xf3\xbeq\r\x8d\xcb\xc0\x8adF\x1dl\xc5\xde
谢谢
你需要使用memoryview
将二进制数据插入sqlite,像这样:
conn.execute("""
INSERT INTO my_acct
(acct_username)
VALUES (?) """, (memoryview(v[0]), )
)
注意:在python 2 中,在这种情况下会使用内置的buffer
。 buffer
在 python 3 中不再可用,已被 memoryview
取代。