2

What I want to do seems like it should be pretty straightforward but I just can't get past the errors I'm getting. Basically, I create a list, create a database table and then want to insert the elements of the list into the table. Here's what I've got:

F_wheel_data = [1,3,1,3,1,3,1,3,1,3,2,1,3,1,3,1,3,1,3,1,3,4]
curs.execute('CREATE TABLE F_wheel (url_num INTEGER NOT NULL)')
curs.executemany('INSERT INTO F_wheel VALUES (?)', F_wheel_data)

And the error I get is:

curs.executemany('INSERT INTO F_wheel VALUES (?)', F_wheel_data)
ValueError: parameters are of unsupported type

What gives?

Jeff F
  • 975
  • 4
  • 14
  • 24
  • Instead of inserting the values all at once, have you tried just inserting one value or looping through your arrays to insert them one at a time? – Takide Jul 31 '15 at 18:00

2 Answers2

4

Looking at this documenation for executemany(), you need to pass a list of tuples.

F_wheel_data = [1,3,1,3,1,3,1,3,1,3,2,1,3,1,3,1,3,1,3,1,3,4]
F_wheel_data = [(i,) for i in F_wheel_data]
dsh
  • 12,037
  • 3
  • 33
  • 51
  • This works but a curious thing is that "2" is the first rowid and and "3" is the first record (not "1")...but all 22 elements of the list were inserted, just not in order of the list of tuples. – Jeff F Jul 31 '15 at 18:50
  • The table you show does not have a rowId column. Data in SQL does not have an intrinsic order, and the database can return the rows in any order unless you specify an `ORDER BY` clause on your query. – dsh Jul 31 '15 at 19:12
  • Oops, my fault. I had code after the above code that rotated the records in the table (deletes the first record and inserts it again...to be the last record). I forgot it was there. – Jeff F Jul 31 '15 at 19:20
0

It looks like executemany() take a list of tuples, not a list of integers.

I am not an SQL expert, but I would try:

F_wheel_data = [(1),(3),(1),(3),...,(4)]
Will
  • 4,299
  • 5
  • 32
  • 50
  • This almost works. For a one element tuple you need to add a comma, like this, [(1,),(3,),(1,),(3,),...,(4,)]. Then, all is well – Jeff F Jul 31 '15 at 19:21