问题描述
简单地说,我正在尝试制作一个 sql 数据库表并将数据输入其中.我让它以更简单的方式工作,但是当我将它放入我的脚本时,它会导致此错误.我希望它的一些简单我错过了.任何帮助/建议将不胜感激.
simply put i am trying to make a sql database table and input data into it. I have it working in a simpler way, but when I put it into my script it results in this error. I'm hoping its something simple I missed. Any help/advice would be greatly appreciated.
conn = sqlite3.connect('Data1.db')
c = conn.cursor()
# Create table
c.execute('''CREATE TABLE Data_Output6
(date text, output6MV real)''')
Averages_norm = []
for i, x in enumerate(Averages):
Averages_norm.append(x*output_factor)
c.execute("INSERT INTO Data_Output6 VALUES (%r,%r)" %(xdates[i],Averages_norm[-1]))
conn.commit()
导致错误:
57 for i, x in enumerate(Averages):
58 Averages_norm.append(x*output_factor)
---> 59 c.execute("INSERT INTO Data_Output6 VALUES (%r,%r)"%(xdates[i],Averages_norm[-1]))
60 conn.commit()
61
OperationalError: near "(": 语法错误
OperationalError: near "(": syntax error
推荐答案
简单地说,让 DB API 进行格式化:
Simply put, let the DB API do that formatting:
c.execute("INSERT INTO Data_Output6 VALUES (?, ?)", (xdates[i], Averages_norm[-1]))
并参考文档 https://docs.python.org/2/library/sqlite3.html 提到的地方:
And refer to the documentation https://docs.python.org/2/library/sqlite3.html where is mentioned:
相反,使用 DB-API 的参数替换.
这篇关于sqlite3 "OperationalError: near "(": syntax error" python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!