Insert list into column of single sqlite database

I have a list of data that needs to be inserted into one database column. I am getting this error when trying to do this:

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

      

This parameter is a list like this:

['\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', ...

(the list continues) ...

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

I am extracting part of a text file like this:

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 would prefer that the data be inserted into the table in such a way that it can be read and listed line by line just like a text file. How should I do it? Should I use a type blob

? How am I supposed to fetch the data differently so it is inserted "as is" without all the tab codes and newline?

+3


source to share


1 answer


You can use pickle . It is a Python module used to serialize an object and store it somewhere. If you are unsure about serialization, read up .

In short, you are converting a data object to binary, sorting it over the network, or storing it in a database as a blob. Then you can use it later.

In your query, it is possible that your object is pickled, store it in a variable such as pdata

and then put it in the desired field of the column column like:



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

      

Also, I just saw that your list contains text data that can be converted to JSON and in this way you can easily dump the JSON of the specified list into the sqllite3 column.

+3


source







All Articles