0

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)) 
4

1 回答 1

2

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.

于 2013-10-24T13:55:21.083 回答