简体   繁体   English

将列表插入单个sqlite数据库列

[英]Insert list into single sqlite database column

I have a list of data that needs to be inserted into a single database column. 我有一个需要插入单个数据库列的数据列表。 I am getting this error when trying to do it: 尝试这样做时我收到此错误:

sqlite3.InterfaceError: Error binding parameter 4 - probably unsupported type.

That parameter is a list as follows: 该参数是一个列表如下:

['\\r\\n', ' \\n', 'Please let me know if you still need Curve Shift.\\n', '\\n', 'Thanks,\\n', 'Heather\\n', ' -----Original Message-----\\n', 'From: \\tAllen, Phillip K. \\n', 'Sent:\\tFriday, December 07, 2001 5:14 AM\\n', 'To:\\tDunton, Heather\\n', 'Subject:\\tRE: West Position\\n', '\\n', 'Heather,\\n', '\\n', 'Did you attach the file to this email?\\n', '\\n', ' -----Original Message-----\\n', ...

(list continues)... (列表继续)......

'\\n', 'Let me know if you have any questions.\\n', '\\n', '\\n', 'Heather']

I extract part of a text file like so: 我提取文本文件的一部分,如下所示:

def extract_values(f):
    lines = f.readlines()

    for line_number, line in enumerate(lines):
        if line.startswith("X-FileName:"):
            data = lines[line_number:]  # read from specified line until end of file
            break

I'd prefer the data be inserted raw into the table so that it can be read out and enumerated line by line just like a text file. 我更喜欢将数据原始插入表中,以便可以像文本文件一样逐行读出和枚举。 How do I do this? 我该怎么做呢? Should I use the blob type? 我应该使用blob类型吗? How should I extract the data differently so it is inserted 'as is' without all the tab and newline codes? 我应该如何以不同方式提取数据,以便在没有所有制表符和换行符代码的情况下“按原样”插入数据?

You can use pickle . 你可以使用泡菜 It is a Python module used to serialize an object and store it somewhere. 它是一个Python模块,用于序列化对象并将其存储在某个地方。 If you are unaware of serialization, please read it up . 如果您不知道系列化,请阅读起来

In short, you convert a data object to binary, marshall it over a network, or store it in a database as a blob. 简而言之,您将数据对象转换为二进制文件,通过网络对其进行编组,或将其作为blob存储在数据库中。 You can then use it later on. 然后您可以在以后使用它。

In your query, you can probably have your object pickled, store it in a variable such as pdata , and then put it in your desired blob column field like: 在您的查询中,您可以将对象腌制,将其存储在变量(如pdata ,然后将其放在所需的blob列字段中,如:

cursor.execute("YOUR SQL QUERY", sqlite3.Binary(pdata))

Also, I just saw that your list contains textual data, which can be converted to JSON and that way, you could easily do a JSON dump of your specified list into the sqllite3 column. 此外,我刚看到您的列表包含文本数据,可以将其转换为JSON,这样,您可以轻松地将指定列表的JSON转储到sqllite3列中。

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

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