sqlite3 - 绑定数量不正确

时间:2018-02-23 02:07:06

标签: python sqlite

我收到此Sqlite3编程错误:sqlite3.ProgrammingError:提供的绑定数量不正确。当前语句使用2,并且提供了0。

我不确定为什么。我已经尝试了我能想到的一切。请协助。

谢谢!

import csv

import sqlite3

with sqlite3.connect("new.db") as connection:

   c = connection.cursor()

   employees = csv.reader(open("employees.csv", "rU"))

   #c.execute("CREATE TABLE employees (firstname TEXT, lastname TEXT)")
   c.executemany("INSERT INTO employees(firstname, lastname) values (?, ?)", employees)

3 个答案:

答案 0 :(得分:0)

c.executemany("INSERT INTO employees(firstname, lastname) values (?, ?)", employees)

您有两个?,但仅提供employees => values (?, ?)", employees)

答案 1 :(得分:0)

csv.reader产生一个生成器表达式。因此,您需要将结果列为清单:

c.executemany("INSERT INTO employees(firstname, lastname) values (?, ?)", list(employees))

答案 2 :(得分:0)

employees需要是2d列表,每次调用executemany一些。您可以轻松地使用嵌套列表推导创建:

import csv
import sqlite3

with sqlite3.connect("new.db") as connection:

   c = connection.cursor()

   reader = csv.reader(open("employees.csv", "rU"))
   employees = [[x for x in row] for row in reader]

   c.executemany("INSERT INTO employees(firstname, lastname) values (?, ?)", employees)