我收到以下错误
Traceback (most recent call last):
File "D:\PYTHON SUPER INP\DB test - Copy1220.py", line 29, in <module>
if __name__ == "__main__":main()
File "D:\PYTHON SUPER INP\DB test - Copy1220.py", line 17, in main
addCust("Zach")
File "D:\PYTHON SUPER INP\DB test - Copy1220.py", line 13, in addCust
VALUES(?)""",(name))
ProgrammingError: Incorrect number of bindings supplied. The current
statement uses 1, and there are 4 supplied.
使用以下代码
import sqlite3
createDb = sqlite3.connect(":memory:")
queryCurs = createDb.cursor()
def createTable():
queryCurs.execute(''' CREATE TABLE customers
(id INTEGER PRIMARY KEY, name TEXT)''')
def addCust(name):
queryCurs.execute("""INSERT INTO customers (name)
VALUES(?)""",(name))
def main():
createTable()
addCust("Zach")
createDb.commit()
queryCurs.execute("SELECT * FROM customers")
for i in queryCurs:
print "/n"
for j in i:
print j
queryCurs.close
if __name__ == "__main__":main()
答案 0 :(得分:4)
下面:
def addCust(name):
queryCurs.execute("""INSERT INTO customers (name)
VALUES(?)""",(name))
当您执行(name)
时,它与name
相同 - 因此如果name
为"Zach"
,那么它就是一个字符串,那就是&#39; s iterable,长度为4,相反,使其成为一个元组或列表,例如:
(name,) # notice the trailing comma
例如:
>>> len(('Zach')) # same as len('Zach')
4
>>> len(('Zach',)) # now a one-tuple containing a string of length 4
1
其他一些注意事项 - 请参阅代码注释:
for i in queryCurs:
print "/n" # <-- to print a new line - use `\n` - not `/n`
for j in i:
print j
queryCurs.close # this should be queryCurs.close() (eg - call the method)