在python中,我想:
我可以处理大多数这些步骤,只有一些我不理解的事情。
这是否可以使用sqlite脚本? python sqlite类能够执行该脚本吗?
conn = sqlite3.connect(central_database, check_same_thread=False)
cur = self.conn.cursor()
#user defines csv file and other params via http form -> script
cur.execute(script)
csv = cur.fetchall()
我可能会想到这一切都错了。所以这是我的设置:
答案 0 :(得分:0)
Haven没有测试过,但你可以试试这个:
conn = sqlite3.connect(central_database, check_same_thread=False)
cur = self.conn.cursor()
# Create table using first row of csv (column names)
#user defines csv file and other params via http form -> script
with open ('test.csv', 'r') as f:
reader = csv.reader(f)
columns = next(reader)
query = 'insert into MyTable({0}) values ({1})'
query = query.format(','.join(columns), ','.join('?' * len(columns)))
for data in reader:
cur.execute(query, data)
cur.commit()
cur.execute(script)
csv = cur.fetchall()
# Delete table
来源: Writing a csv file into SQL Server database using python