我有一个Python列表类型的数据无法插入Python列表的数据到数据库
我想插入这个名单到数据库表 - 在一排这样的
country = [('a', 3), ('b', 2), ('c', 1)]
所以我已经写代码:
import sqlite3
from collections import Counter
db = "test.db"
conn = sqlite3.connect(db)
c = conn.cursor()
c.execute("CREATE TABLE country_data (country TEXT)")
country = ['a', 'a', 'a', 'b', 'b', 'c']
c.execute("INSERT INTO country_data VALUES(?)", (str(list(Counter(country).items()))))
conn.commit()
但它给我的错误
Traceback (most recent call last):
File "try.py", line 19, in <module>
c.execute("INSERT INTO country_data VALUES(?)", (str(list(Counter(country).items()))))
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 30 supplied.
我该如何解决这个问题?
嗯,我可以告诉你错误至少来自哪里。由该表达式返回的字符串的长度是30.所以我猜它不期待这样的字符串。不幸的是,我不太了解sqlite3库以告诉你它需要什么。 – user3030010
如果我删除STR()和只写 '名单(计数器(国家).items())' 错误是: 'sqlite3.ProgrammingError:提供的绑定数有误。当前语句使用1,并且提供了3个.' –
您的表格由恰好一列组成。你希望如何在一列中存储'('a',3)'?或者你的意思是你想要三行'a',两行'b'和一行'c'? –