I can't get Python's executemany for sqlite3 to work properly

前端 未结 2 1806
醉酒成梦
醉酒成梦 2020-12-09 18:05

I was trying to use executemany to insert values into a database, but it just won\'t work for me. Here is a sample:

clist = []
clist.append(\"abc\")
clist.ap         


        
2条回答
  •  执念已碎
    2020-12-09 18:29

    From what I know of executemany, you meant,

    clist = [("abc", ), ("def", ), ("ghi", )]
    cursor.executemany("INSERT INTO myTable(data) values(?)", clist)
    

    Or something similar. Don't quote me on the syntax for sqlite, I haven't used it in an app in a while, but you need an iterable of tuples (more generally iterables).

    It looks like the error you're getting is that it's trying to iterate through each string you're providing, so your statement works like:

    clist = [('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'h', 'i')]
    

    I don't know what your second query is trying to accomplish, but it appears to address a different table, so I'm guessing off of no schema info, but if you change the single character strings to multicharacter strings, it will fail too.

提交回复
热议问题