【问题标题】:'more placeholders than values' when inserting into sqlite database插入 sqlite 数据库时“占位符多于值”
【发布时间】:2020-12-04 15:12:46
【问题描述】:

我正在尝试从 python 动态插入到 sqlite 数据库。这是我的代码:

for person in people:
db.execute("INSERT INTO students (first, middle, last, house, birth) VALUES (?, ?, ?, ?, ?)", (person[0], person[1], person[2], person[3], person[4])

Python 正在返回错误:

  File "import.py", line 34, in <module>
    db.execute("INSERT INTO students (first, middle, last, house, birth) VALUES (?, ?, ?, ?, ?);", (person[0], person[1], person[2], person[3], person[4]))
  File "/usr/local/lib/python3.7/site-packages/cs50/sql.py", line 21, in decorator
    return f(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/cs50/sql.py", line 186, in execute
    raise RuntimeError("more placeholders ({}) than values ({})".format(_placeholders, _args))
RuntimeError: more placeholders (?, ?, ?, ?, ?) than values ('Adelaide', 'NULL', 'Murton', 'Slytherin', 1982)

当我在 python 之外运行一个普通查询时它就可以工作。任何帮助将不胜感激。

【问题讨论】:

  • 我不是很了解python,但是你试过命名查询参数吗?它可能会帮助您找出导致问题的参数。
  • 另外我认为你可能需要去掉这里的括号 (person[0], person[1], person[2], person[3], person[4])
  • 你不应该把 'NULL' (string) 当作 Null ,这会让 Null 先生和夫人非常恼火bbc.com/future/article/…

标签: python sqlite sql-insert


【解决方案1】:

另外我认为你可能需要去掉这里的括号 (person[0], person1, person[2], person[3], person[4]) 这解决了它!谢谢Sebastien

更新的代码有效:

insert_query = "INSERT INTO students (first, middle, last, house, birth) VALUES (?, ?, ?, ?, ?)"

for person in people:
    db.execute(insert_query, person[0], person[1], person[2], person[3], person[4])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-09
    • 2011-02-04
    • 1970-01-01
    • 1970-01-01
    • 2014-06-15
    • 1970-01-01
    • 2020-04-30
    • 2014-08-19
    相关资源
    最近更新 更多