简体   繁体   中英

Insert values from list into sqlite3 database

I try to insert values from variables and values from a list into a sqlite3 database.

Problem: The list is not unpacked, it is used as a single element. What do I miss? Do I have to unpack the list into variables? In the example list_element is a list with six elements

    cur.execute("INSERT INTO websites VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
               ('NULL', qry, strtm, list_element, COUNTER, SPAM, EXCERPT, COLLECTION)) 

Concatenate the sequences:

cur.execute("INSERT INTO websites VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
           [None, qry, strtm] + list_element + [COUNTER, SPAM, EXCERPT, COLLECTION]) 

I built a list here as it is easier to concatenate lists and lists than it is to concatenate tuples and lists (you'd have to convert list_element to a tuple first).

Note that I used None instead of 'NULL' ; None is translated to a SQL NULL by Python database adapters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM