I'm attempting to update my sqlite db with 2 python lists. I have a sqlite db with three fields. Name, number, date. I also have three python lists with similar names. I'm trying to figure out a way to update my sqlite db with data from these 2 lists. I can get the db created, and even get a single column filled, but I cant seem to update it correctly or at all. Is there a way to INSERT both lists at once? Rather than INSERT a single column and then UPDATE the db with the other?
Here is what I have so far:
name_list = []
number_list = []
date = now.date()
strDate = date.strftime("%B %Y")
tableName = strDate
sqlTable = 'CREATE TABLE IF NOT EXISTS ' + tableName + '(name text, number integer, date text)'
c.execute(sqlTable)
conn.commit()
for i in name_list:
c.execute('INSERT INTO January2018(names) VALUES (?)', [i])
conn.commit()
I can't seem to get past this point. I still need to add another list of data (number_list) and attach the date to each row.
Here's what I have on that:
for i in number_list:
c.execute('UPDATE myTable SET number = ? WHERE name', [i])
conn.commit()
Any help would be much appreciated. And if you need more information, please let me know.
c.execute('INSERT INTO January2018(names) VALUES (?)', ([i],)). Does that work?for loopto iterate through the second list? So likefor i in name_list: for o in number_list: c.execute('INSERT INTO January2018 VALUES (?, ?)', ([i], [o])) conn.commit()executemanymethod given by Ajax and supply asc.executemany('INSERT INTO January2018 VALUES (?, ?)', list(zip(name_list, number_list)))executemanybecause it allows you to provide nested lists and execute the same query for each set of values in one go. Also, an aside, there is no need for you tocommit()on every iteration of your loop, just do it once at the end (unless there's some pressing reason not to) because it will speed up the execution significantly.