python - sqlite3 "OperationalError: near "(": syntax error" python

标签 python sqlite

简单地说,我正在尝试创建一个 SQL 数据库表并将数据输入其中。我让它以更简单的方式工作,但是当我将它放入我的脚本中时,它会导致此错误。我希望它是我错过的简单的东西。任何帮助/建议将不胜感激。

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 "(": 语法错误

最佳答案

简单地说,让 DB API 进行格式化:

c.execute("INSERT INTO Data_Output6 VALUES (?, ?)", (xdates[i], Averages_norm[-1]))

并引用文档https://docs.python.org/2/library/sqlite3.html提到的地方:

Instead, use the DB-API’s parameter substitution.

关于python - sqlite3 "OperationalError: near "(": syntax error" python ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30257826/

相关文章:

python 的 re : replace regex to regex

python - 为什么在此 Python 代码中允许使用大括号?

android - 在 TextView 中显示 SQLite 查询的总和值

c# - 带有 winforms 部署的 sqlite 数据库

java - 从给定的电子邮件中检索一些列

linux - 嵌入式 Linux NOR MTD Flash 中的 SQLite3

python - 是否可以从yarn激活venv?

python - 从现有的日期列创建新列 "Week"

java - 在 android studio : This class should provide default constructor 中构建签名的 apk 时出错

python - 无法发现使用 PyBluez 创建的服务