跟踪错误和KeyError,似乎无法正常工作。有任何想法吗?

时间:2016-02-14 12:31:52

标签: python sql sqlite

我试图让下面的代码工作,通过从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()`

0 个答案:

没有答案