我试图让下面的代码工作,通过从csv文件中提取信息到sqlite数据库,当我尝试执行时,我不断得到这个键错误。
Traceback (most recent call last):
File "test.py", line 10, in <module>
to_db = [(i['iddd'], i['Country'], i['StateCode']) for i in dr]
KeyError: 'iddd'
我的代码:
import csv, sqlite3
con = sqlite3.connect('Movies.db')
cur = con.cursor()
cur.execute('DROP TABLE IF EXISTS Info;')
cur.execute("CREATE TABLE Info(iddd INT PRIMARY KEY, Country INT, StateCode TEXT);")
with open('practice1.csv', 'rU') as fin:
dr = csv.DictReader(fin)
to_db = [(i['iddd'], i['Country'], i['StateCode']) for i in dr]
cur.executemany("INSERT INTO Info (iddd, Country, StateCode) VALUES (?, ?, ?);", to_db)
con.commit()`