import csv
import sqlite3
fileName = 'australianpublicholidays.csv'
accessMode = 'r'
# Create a database in RAM
holidayDatabase = sqlite3.connect(':memory:')
# Create a cursor
c = holidayDatabase.cursor()
# Create a table
c.execute('''CREATE TABLE holidays
(date text, holidayName text, information text, moreInformation text, applicableTo text)''')
# Read the file contents in to the table
with open(fileName, accessMode) as publicHolidays :
listOfPublicHolidays = csv.reader(publicHolidays)
for currentRow in listOfPublicHolidays :
for currentEntry in currentRow :
c.execute('INSERT INTO holidays VALUES (?, ?, ?, ?, ?)', currentEntry)
# Close the database
holidayDatabase.close()
下一行
c.execute('INSERT INTO holidays VALUES (?, ?, ?, ?, ?)', currentEntry)
是导致此错误的原因
Incorrect number of bindings supplied. The current statement uses 5, and there are 4 supplied.
我已通过删除嵌套for循环来更正错误
但是我仍然不清楚错误的原因。你知道吗
currentRow
已经是一个序列。这是行中所有字段的列表。 如果您要打印出currentRow
,您将得到如下输出(假设这是您的数据集https://data.gov.au/dataset/australian-holidays-machine-readable-dataset):当你这么做的时候
实际上,您得到的是列表中第一个元素中所有字符的列表。 因为没有跳过标题行,所以实际上得到的是单词“Date”中的字符列表。等于4个字符并导致错误:
如果使用
next(listOfPublicHolidays, None)
跳过标题行,如:您将收到以下错误消息,因为
currentEntry
将是“20150101”中的字符列表,长度为8:这就是为什么移除
for currentEntry in currentRow :
块并将其重写为:注意:在我的机器上,出现以下错误:
因此,我将你的剧本改写如下,以处理上述问题:
相关问题 更多 >
编程相关推荐