[英]Unable to insert binary data from mssql into sqlite3 using python
我正在嘗試查詢Microsoft SQL 2005數據庫,處理數據並將密鑰寫入更新后的數據,這些數據已更新到本地計算機上的sqllite3數據庫中。
我使用的工具是python 2.7,pyodbc和sqllite3。 我在Windows 7上,並且我嘗試使用帶pyodbc的sql服務器和sql native客戶端驅動程序連接到我的mssql db的驅動程序,並且都產生相同的結果。
我遇到的字段是mssql中的binary(16)的uuid字段。 當我使用pyodbc查詢數據時,它將以bytearray()的形式返回給我。
這是我運行以下命令時得到的輸出。
id = mycursor.fetchone()
print id
àO÷ eÅO‹1ÝWt'E
repr(id)
字節數組(b'\\ xe0O \\ xf7 \\ x1d \\ x9de \\ xc5O \\ x8b1 \\ x0e \\ xddWt \\ x91E')
當我將該ID插入sqllite3數據庫時,得到以下信息。
s = sqlite3.connect('tmp.db')
cursor = s.cursor()
s.execute("create table recs(uuid blob)")
s.commit()
s.execute("insert into recs (uuid) values(?)", id)
1 s.execute(“插入記錄(uuid)值(?)”,id
ProgrammingError:提供的綁定數不正確。 當前語句使用1,並且提供了16。
Python值必須是一個buffer
對象:
>>> s.execute("insert into recs (uuid) values(?)", (buffer(id), ))
<sqlite3.Cursor object at 0x011CD2E0>
>>> r = s.execute("select * from recs").fetchone()
>>> r
(<read-write buffer ptr 0x011F9280, size 16 at 0x011F9260>,)
>>> r[0]
<read-write buffer ptr 0x011F9280, size 16 at 0x011F9260>
>>> str(r[0])
'\xe0O\xf7\x1d\x9de\xc5O\x8b1\x0e\xddWt\x91E'
>>> str(r[0]) == id
True
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.