简体   繁体   中英

How do I correctly map a Python “list of lists” into an SQL table (built with SQLite3)?

I have data coming out of an XML parser that contains instrument readings. Samples of the data are reproduced below:

cimReading = [['2012-08-15 10:05:13.101485', ['0x46'], ['0x32'], ['1.234'], ['5.678'], ['9.123'],
 ['4.567'], ['0x98'], ['0x97']], 
['2012-08-15 10:05:13.101979', ['0x47'], ['0x33'], ['8.901'], ['2.345'], ['6.789'], 
['0.123'], ['0x96'], ['0x95']]]

I'm trying to loop over the list items and put them away in a table, cim76_dmt that was previously defined in Python with sqlite3 . Here is the code I'm using to try to do this:

for devList in cimReading:
    print devList
    cim76TabPopulate.execute('INSERT INTO cim76_dmt VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', 
            devList)
BenchDB.commit()
cim76TabPopulate.close() 

Here is the error message I receive upon execution:

['2012-08-15 10:40:21.110140', ['0x46'], ['0x32'], ['1.234'], ['5.678'], ['9.123'], ['4.567'], ['0x98'], ['0x97']]
EXCEPTION...
(<class 'sqlite3.InterfaceError'>, InterfaceError('Error binding parameter 1 - probably unsupported type.',), <traceback object at 0x1007a7518>)

So, I'm having a problem mapping a list item to a SQL table field. I think there should be a way to do this, but another possibility may be to tweak the XML parsing so I generate a single list of multiple strings for each record. I'd welcome any suggestions or advice on this.

Thanks in advance!

Your problem is that all the parameters after the first one are lists, not strings or integers. The part win the error message that is complaining about unsupported type is complaining about the data element ['0x46'] .

You need to massage your data a little bit more; you want to unpack ['0x46'] to '0x46' . This needs to be done to the other data elements in single length lists in your example data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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