1

my code the error points to looks like this

for i in array1:
   cursor.execute("insert into Recipe values (?,?,?)",(array1[i], array2[i], array3[i]))
   db.commit()

the contents of the arrays are strings, so how would I put the arrays into the sqlite3 table?

quantumdisaster
  • 567
  • 2
  • 6
  • 12

2 Answers2

2

Use zip() to iterate over all three arrays together:

for row in zip(array1, array2, array3):
    cursor.execute("insert into Recipe values (?,?,?)", row)
db.commit()
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
  • this is perfect...almost, I just get this error Traceback (most recent call last): File "testread.py", line 21, in cursor.execute("insert into Recipes values (?,?,?) ",row) sqlite3.ProgrammingError: You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings (like text_factory = str). It is highly recommended that you instead just switch your application to Unicode strings. Any ideas? – quantumdisaster Nov 27 '11 at 04:23
  • @quantumdisaster: I presume you've read http://stackoverflow.com/questions/2838100/pysqlite2-programmingerror-you-must-not-use-8-bit-bytestrings and it hasn't helped? – johnsyweb Nov 27 '11 at 04:26
  • right, it gives me an error about .decode not being a part of row – quantumdisaster Nov 27 '11 at 04:41
  • now I got that error fixed its bitching about only 1 binding being supplied when I called for 3 I changed the line of code to cursor.execute("insert into Recipe values (?,?,?)",(u'row',)) – quantumdisaster Nov 27 '11 at 04:55
  • These questions are a long way away from the one you asked! However, if I point out that `(u'row',)` is a tuple containing the unicode string "row", not the tuple containing elements from your three arrays, that should help you on your way... – johnsyweb Nov 27 '11 at 05:11
0

You want to use something like this:

for i in range(len(array1)):

...but that's not quite as idiomatic as it should be.

Brian Cain
  • 14,403
  • 3
  • 50
  • 88