简体   繁体   English

将数组插入到sqlite3 python中

[英]Inserting an array into sqlite3 python

I have a list/array of strings:我有一个字符串列表/数组:

l = ['jack','jill','bob']

Now I need to create a table in slite3 for python using which I can insert this array into a column called "Names".现在我需要在 slite3 中为 python 创建一个表,我可以使用它将此数组插入到名为“Names”的列中。 I do not want multiple rows with each name in each row.我不希望每行中的每个名称都有多个行。 I want a single row which contains the array exactly as shown above and I want to be able to retrieve it in exactly the same format.我想要一行包含完全如上所示的数组,并且我希望能够以完全相同的格式检索它。 How can I insert an array as an element in a db?如何将数组作为元素插入数据库中? What am I supposed to declare as the data type of the array while creating the db itself?在创建数据库本身时,我应该将什么声明为数组的数据类型? Like:像:

c.execute("CREATE TABLE names(id text, names ??)")

How do I insert values too?我如何插入值? Like:像:

c.execute("INSERT INTO names VALUES(?,?)",(id,l))

EDIT: I am being so foolish.编辑:我太愚蠢了。 I just realized that I can have multiple entries for the id and use a query to extract all relevant names.我刚刚意识到我可以有多个 id 条目并使用查询来提取所有相关名称。 Thanks anyway!不管怎么说,还是要谢谢你!

You can store an array in a single string field, if you somehow genereate a string representation of it, eg sing the pickle module.您可以将数组存储在单个字符串字段中,如果您以某种方式生成它的字符串表示,例如唱pickle模块。 Then, when you read the line, you can unpickle it.然后,当您阅读该行时,您可以解开它。 Pickle converts many different complex objects (but not all) into a string, that the object can be restored of. Pickle 将许多不同的复杂对象(但不是全部)转换为可以恢复对象的字符串。 But: that is most likely not what you want to do (you wont be able to do anything with the data in the tabel, except selecting the lines and then unpickle the array. You wont be able to search.但是:这很可能不是您想要做的(您将无法对表格中的数据做任何事情,除了选择行然后解开数组。您将无法搜索。

If you want to have anything of varying length (or fixed length, but many instances of similiar things), you would not want to put that in a column or multiple columns.如果您想要任何长度可变(或固定长度,但类似事物的许多实例)的东西,您不希望将其放在一列或多列中。 Thing vertically, not horizontally there, meaning: don't thing about columns, think about rows.垂直放置,而不是水平放置,意思是:不要考虑列,考虑行。 For storing a vector with any amount of components, a table is a good tool.对于存储具有任意数量组件的向量,表格是一个很好的工具。

It is a little difficult to explain from the little detail you give, but you should think about creating a second table and putting all the names there for every row of your first table.从您提供的小细节来解释有点困难,但是您应该考虑创建第二个表并将所有名称放在第一个表的每一行中。 You'd need some key in your first table, that you can use for your second table, too:您的第一个表中需要一些键,您也可以将其用于第二个表:

c.execute("CREATE TABLE first_table(int id, varchar(255) text, additional fields)")
c.execute("CREATE TABLE names_table(int id, int num, varchar(255) name)")

With this you can still store whatever information you have except the names in first_table and store the array of names in names_table , just use the same id as in first_table and num to store the index positions inside the array.有了这个,您仍然可以在first_table存储除名称以外的任何信息,并将名称数组存储在names_table ,只需使用与first_tablenum相同的id来存储数组内的索引位置。 You can then later get back the array by doing someting like然后,您可以稍后通过执行类似操作来取回数组

SELECT name FROM names_table 
WHERE id=?
ORDER BY num

to read the array of names for any of your rows in first_table .读取first_table任何行的名称数组。

That's a pretty normal way to store arrays in a DB.这是在数据库中存储数组的一种非常正常的方式。

This is not the way to go.这不是要走的路。 You should consider creating another table for names with foreign key to names.您应该考虑为具有名称外键的名称创建另一个表。

You could pickle/marshal/json your array and store it as binary/varchar/jsonfield in your database.您可以 pickle/marshal/json 您的数组并将其存储为 binary/varchar/jsonfield 在您的数据库中。

Something like:就像是:

import json

names = ['jack','jill','bill']
snames = json.dumps(names)
c.execute("INSERT INTO nametable " + snames + ";")

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

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