grigs
grigs

Reputation: 1150

Upload Python list to sqlitedb

I'm having some trouble uploading lists to my SQLite db. I would love to be able to upload two lists into my table at once, but for the purposes of this question, I have created a table with one text field called 'names'.

Here is what I have so far:

names = ['Jim', 'Grace', 'Francine', 'George',]

For i in names:
    c.executemany('INSERT INTO mytable values(?)', i)
    conn.commit()

Instead of this being uploaded to my table:

Jim
Grace
Francine
George

I get:

J
i
m
F
r
a
n
c
etc etc

Maybe I'm missing something, but when I run:

For i in names:
   print(i)

I get the correct output:

Jim 
Grace
Francine
George

Am I correct in assuming I have to iterate over the list in order to upload the entire thing to my sqlitedb? It's been a couple of months since I touched python, so maybe I'm missing something. Any help would be much appreciated!

Also, if you need more information, just let me know.

Upvotes: 0

Views: 18

Answers (1)

Bill Bell
Bill Bell

Reputation: 21663

Replace the statement that includes the INSERT:

  • You're not executing 'many'.
  • The stuff you're inserting needs to be in a list.

c.execute('insert into mytable values(?)', [i])

Upvotes: 1

Related Questions