从csv填充sqlite3数据库:语法错误'?'

时间:2017-02-27 18:19:54

标签: python csv sqlite

在上一篇关于python中csv文件中sqlite3数据库填充的帖子之后,我已经完全按照编写的方式使用了代码,但不断提出:Traceback(最近一次调用最后一次):

File "Z:/KS4/Computer Science/OCR corsework/Task 1 Database/populate.py", line 10, in <module>
    cursor.execute(query, data)
sqlite3.OperationalError: near "?": syntax error

这是代码:

import csv, sqlite3
connection = sqlite3.connect("TutorGroup.db")

with open ('studentsEmail-master.csv', 'r') as f:
    r = csv.reader(f)
    data = next(r) 
    query = 'insert into dbo.students ({0})'
    query = query.format(','.join('?' * len(data)))
    cursor = connection.cursor()
    cursor.execute(query, data)
    for data in reader:
        cursor.execute(query, data)
    cursor.commit()

1 个答案:

答案 0 :(得分:1)

https://www.sqlite.org/lang_insert.html

您需要在查询中添加values字样:

query = 'insert into dbo.students values ({0})'
#                                 ^^^^^^