简体   繁体   English

如何正确地将Python“列表列表”映射到SQL表(使用SQLite3构建)?

[英]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. 我的数据来自包含仪器读数的XML解析器。 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 . 我正在尝试遍历列表项并将它们放在一个表cim76_dmt ,该表先前在Python中使用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. 所以,我在将列表项映射到SQL表字段时遇到问题。 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. 我认为应该有一种方法可以做到这一点,但另一种可能性是调整XML解析,因此我为每条记录生成一个包含多个字符串的列表。 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'] . 部分赢得错误消息,抱怨unsupported type抱怨数据元素['0x46']

You need to massage your data a little bit more; 你需要更多地按摩你的数据; you want to unpack ['0x46'] to '0x46' . 你想将['0x46']解包为'0x46' This needs to be done to the other data elements in single length lists in your example data. 这需要对示例数据中单个长度列表中的其他数据元素进行。

暂无
暂无

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

相关问题 如何使用python在SQLite3中显示表结构? - How do I display the table structure in SQLite3 with python? 我如何在 python 中转储单个 sqlite3 表? - how do i dump a single sqlite3 table in python? Python sqlite3-如何制定条件,检查表中的属性是否为列表中的元素之一 - Python sqlite3 - How do I formulate a condition where we check if an attribute in the table is one of the elements in my list 如何使用 SQLite3 和 Python 更新 SQLite 表中的行 - How to do update rows in SQLite table using SQLite3 and Python python sqlite3 tkinter-如何为sqlite表中的每一行自动创建新的小部件? - python sqlite3 tkinter- How do I automatically create new widgets for every row in an sqlite table? 我有一个使用HTML,CSS和Javascript构建的静态网站。 如何将其与使用Python API访问的SQLite3数据库集成? - I have a static website built using HTML, CSS and Javascript. How do I integrate this with a SQLite3 database accessed with the Python API? 如何将数据写入从Python中的SQL数据库获取的文件中? (sqlite3的) - How do I write data to a file that I have taken from an SQL database in Python? (sqlite3) Python,SQLite3:如何在表的单元格中插入整数列表 - Python, SQLite3: How to insert a list of integers into a cell in the table 如何从列表创建sqlite3表列? (在python中) - How to create sqlite3 table columns from a list? (in python) 如何使用python将文本文件导入SQLite3表 - How do I import a text file into a SQLite3 table using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM