dimanche 13 septembre 2015

Brief syntax for inserting row into sqlite database using python sqlite3

I have a csv file of size 360x120 that I want to import into my sqlite database row by row. For one row, I know that below syntax works if mytuple has 2 elements:

import sqlite3
conn = sqlite3.connect(dbLoc)
cur = conn.cursor()
mytuple = (a, b, c, ...) #some long tuple of 120 elements
cur.execute('INSERT INTO tablename VALUES (?, ?)', mytuple)

Problem is, my rows contain 120 columns and I can't really go type 120 question marks into the cur.execute() line. Actually I have, it works but yeah, it is not a good solution. One thing I have tried was:

cur.execute('INSERT INTO tablename VALUES ?', mytuple)

Thought it would just do ?=mytuple and replace ? with mytuple but it doesn't do that. A user comment on the article http://ift.tt/1iku997 shows such syntax, which would work for me but it does not:

t = ('RHAT',)
c.execute('SELECT * FROM stocks WHERE symbol=?', t)

As seen here he's able to replace a tuple into the execute string with a single ? used. How can I achieve the same with INSERT INTO tablename?

Aucun commentaire:

Enregistrer un commentaire