每次我運行它通過python解釋器它寫入新的值。例如:插入或替換語句不適用於sqlite3和python
name = ben
age = 10
phone = 42045042
如果我運行它10次。我的數據庫中有10個重複項。我知道它必須是一個簡單的解決方案,但我一直在這個工作幾個小時,無法弄清楚。
conn = sqlite3.connect('addressbook.db')
cur=conn.cursor()
conn.execute('''
CREATE TABLE IF NOT EXISTS people(name TEXT,
age TEXT, phone TEXT, fblink TEXT)''')
conn.execute("INSERT OR REPLACE INTO people values (?, ?, ?, ?)", ben.displayPerson())
cursor = conn.execute("SELECT name, age, phone, fblink from people")
for row in cursor:
print "NAME = ", row[0]
print "AGE = ", row[1]
print "PHONE = ", row[2]
print "FACEBOOK LINK = ", row[3], "\n"
cur.close()
conn.commit()
conn.close()
你爲什麼使用'INSERT OR REPLACE INTO ...'?它不應該只是「INSERT INTO」嗎? – Sazid
插入到給我同樣的問題,這就是爲什麼我試圖插入或替換。 – stephan