2016-03-18 37 views
0

我正在報廢一些websties並希望將這些數據插入到sqlite3數據庫。我成功創建了連接表和數據庫使用python在sqlite3中不支持的類型錯誤

conn.execute(''' 
    CREATE TABLE datatable (id INTEGER PRIMARY KEY, Date TEXT, 
         Time TEXT, html_link BLOB, Desc BLOB, Tup BLOB) 
''') 

id is the auto_incriment value 
date="03-09-2016" 
time="12:34:56" 
html="http://web1.com" 
des="Oct 21, 2010 - In the code below row is a tuple of 200 elements (numbers) and listOfVars is a tuple of 200 strings that are variable names in the testTable ." 
arr=(1,2,3) 

然後我試圖插入值,以該表

conn.execute('''INSERT INTO datatable (Date, Time, html_link, Desc, Tup) 
        VALUES(?,?,?,?,?)''', (date,time,html,des,arr)) 

但其引發此錯誤

VALUES(?,?,?,?,?)''', (date,time,html,des,arr)) sqlite3.InterfaceError: Error binding parameter 4 - probably unsupported type.

我知道我犯了一些錯誤將適當的數據類型分配給sqlite表。如何找到導致錯誤的列以及如何解決此問題。

+0

那個參數的類型是什麼? –

回答

1

嘗試保存您試圖用泡菜存儲的元組。這是一個工作示例。

import sqlite3 
import pickle 

conn = sqlite3.connect(":memory:") 
conn.text_factory=str 
cur=conn.cursor() 
cur.execute(''' 
    CREATE TABLE datatable (id INTEGER PRIMARY KEY, Date TEXT, 
         Time TEXT, html_link BLOB, Desc BLOB, Tup BLOB) 
''') 

date="03-09-2016" 
time="12:34:56" 
html="http://web1.com" 
des="Oct 21, 2010 - In the code below row is a tuple of 200 elements (numbers) and listOfVars is a tuple of 200 strings that are variable names in the testTable ." 
arr=(1,2,3) 



cur.execute('''INSERT INTO datatable (Date, Time, html_link, Desc, Tup) VALUES(?,?,?,?,?)''', (date,time,html,des,pickle.dumps(arr,2))) 
cur.execute('''SELECT * from datatable''') 
print pickle.loads(cur.fetchall()[0][5]) 
conn.close() 
+0

它返回這個錯誤'TypeError:預期的緩衝區對象' – Eka

相關問題