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

前端 未结 2 1815
醉酒成梦
醉酒成梦 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:41

    Just to complement the context: In a closely related situation I meant to insert a list of poly-tuples into a table using executemany as such:

    res = [("John", "2j4o1h2n"), ("Paula", "lsohvoeemsy"), ("Ben", "l8ers")]
    
    cur.executemany("INSERT INTO users (user, password) VALUES (?)", res)
    

    Expecting SQLite to to take one tuple at a time (hence the single ? parameter-substitution in the VALUES field) and splitting it up into its encapsulated attributes (, in this case), it failed with a sqlite3.ProgrammingError exception The current statement uses 1, and there are 2 supplied. as well, as SQLite expects separately substituted attributes in the VALUES (...) field. So this fixes it:

    cur.executemany("INSERT INTO users (user, password) VALUES (?, ?)", res)
    

    This is a trivial case but might confuse a little, I hope it might help whoever's stuck.

提交回复
热议问题