简体   繁体   English

将列表中的值插入sqlite3数据库

[英]Insert values from list into sqlite3 database

I try to insert values from variables and values from a list into a sqlite3 database. 我尝试将列表中的变量和值的值插入到sqlite3数据库中。

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 在示例中, list_element是包含六个元素的列表

    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). 我在这里建立了一个列表,因为它更容易连接列表和列表,而不是连接元组和列表(你必须list_element转换为元组)。

Note that I used None instead of 'NULL' ; 请注意,我使用None而不是'NULL' ; None is translated to a SQL NULL by Python database adapters. Python数据库适配器将None转换为SQL NULL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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