简体   繁体   English

将列表插入python3 sqlite3中的一行

[英]insert list into one row in python3 sqlite3

So suppose I made an sqlite3 database in which I made a table 因此,假设我创建了一个sqlite3数据库,其中创建了一个表

c.execute("CREATE TABLE tablnam(thing TEXT, thing2 TEXT, thing3 TEXT)")

then I made a list 然后我做了一个清单

list = ["a","b","c"]

How would I go about inserting that one list into tablnam in just one row. 我该如何将那一个列表插入tablnam的一行中。 I've tried 我试过了

c.execute("insert into this values(?)",list)

but it returns Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: parameters are of unsupported type 但它返回Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: parameters are of unsupported type

so to clarify for those who don't understand: I want to insert a lists values, into ONE row in an sqlite3 table in python thx! 因此,对于那些不了解的人可以进行澄清:我想将列表值插入python thx的sqlite3表的ONE行中!

Well buddy, that can't work, you are trying to put a list where a text belongs. 伙计,这行不通,您正在尝试列出 文本所属的列表 Try doing it that way: 尝试这样做:

c.execute("INSERT INTO tablenam(thing, thing2, thing3) VALUES (?, ?, ?)",list[0], list[1], list[2])

Differences to your suggestions: 与您的建议有所不同:

  • I am putting a single string into each text field 我在每个文本字段中输入一个字符串
  • you can only insert exactly three list items per row. 您每行只能插入三个列表项。
  • I've changed your SQL Syntax to contain tablenam and also put in the column names 我已经更改了您的SQL语法,使其包含tablenam并也将其放在列名中
  • changed the SQL style: keywords in allcaps 更改了SQL样式:allcaps中的关键字

Note that you can't (and shouldn't) put an entire list into a single row in a DB. 请注意,您不能也不应该)将整个列表放入数据库的单行中。 If you have to do something along those lines, have a look at foreign keys. 如果您必须按照这些原则进行操作,请查看外键。

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

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