鍍金池/ 問(wèn)答/Python  數(shù)據(jù)庫(kù)/ sqlite3 插入數(shù)據(jù)問(wèn)題

sqlite3 插入數(shù)據(jù)問(wèn)題

使用Python的sqlite3插入數(shù)據(jù)時(shí)報(bào)錯(cuò)


錯(cuò)誤如下
Traceback (most recent call last):
  File "test.py", line 22, in <module>
    cursor.execute('insert into words (id, word, mean) values (%d, \'%s\', \'%s\')' %(i, word_a, mean_a))
sqlite3.OperationalError: near "f": syntax error

代碼的22行如下
cursor.execute('insert into words (id, word, mean) values (%d, \'%s\', \'%s\')' %(i, word_a, mean_a))

問(wèn)題是我的代碼里并沒(méi)有“f”,求大佬指點(diǎn)迷津。
回答
編輯回答
祈歡

改成這樣試試:cursor.execute("insert into words (id, word, mean) values (%d, '%s', '%s')" %(i, word_a, mean_a))

2017年1月25日 19:32
編輯回答
貓小柒
print("insert into words (id, word, mean) values (%d, '%s', '%s')" % (i, word_a, mean_a))

打出來(lái)看看,把報(bào)錯(cuò)那句話打出來(lái), 我估計(jì)是word_a或者mean_a中有英文單引號(hào)造成的,
可以寫(xiě)成下面的樣子

cursor.execute('insert into words (id, word, mean) values (?, ?, ?)', (i, word_a, mean_a))
# 或者
cursor.execute('insert into words (id, word, mean) values (:id, :word, :mean)', {"id": i, "word": word_a, "mean": mean_a})

參考https://docs.python.org/3/lib...

2017年9月4日 20:21