2016-11-17 68 views
0

我試圖從整數列表創建SQLite中一列,但出現以下錯誤:如何從列表添加到數據庫?

"sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 4 supplied." 

這裏是我的代碼:

conn = sq.connect('test.db') 
c = conn.cursor() 

def create_table(): 
c.execute('CREATE TABLE IF NOT EXISTS table(listItem VALUE)') 


def data_entry(): 
x = 0 
list1 = [61,33,4,5] 
n = [l for l in list1] 
while x < len(list1): 
    listItem = n 
    c.execute("INSERT INTO tell(listItem) VALUES (?)", 
      (listItem)) 
    x += 1 
conn.commit() 


create_table() 
data_entry() 
c.close() 
conn.close() 

提前感謝! :)

回答

2

如果我理解正確的話,要循環的列表,而不是插入整個列表一行

for l in list1: 
    c.execute("INSERT INTO tell(listItem) VALUES (?)", 
     (l,)) 
c.close() 
conn.commit() 

而且你的CREATE TABLE語句應創建tell,因爲table是保留字,這就是你插入到

+0

這樣做的伎倆 - 非常感謝你! :) –

相關問題