我正在尝试解析XML文件并将其导入SQLITE数据库。
XML看起来像这样:
<resultset>
<row>
<column name="pct_lucru">unit name</column>
<column name="cod_comercial">00032749</column>
<column name="denumire_med">stuff name</column>
<column name="producator">fabri</column>
<column name="tip_produs">koops</column>
<column name="tva">24.000000</column>
<column name="umc">1</column>
<column name="furnizor">FURNIZORI DIVERSI</column>
<column name="data_expirarii">2015-12-31</column>
<column name="lot">80063</column>
<column name="cant_fl">1</column>
<column name="fractie">0</column>
<column name="cantitate">1</column>
<column name="pret_intr">62.930000</column>
<column name="val_intr">62.930000</column>
<column name="pret_fl">82.720000</column>
<column name="valoare">82.720000</column>
</row>
</resultset>
我有以下python代码
import xmltodict
import sqlite3
conn = sqlite3.connect("test.sqlite")
c = conn.cursor()
with open("export.xml") as fd:
obj = xmltodict.parse(fd.read())
for row in obj["resultset"]["row"]:
for column in row["column"]:
c.execute("INSERT INTO stocks ? VALUES '?'", [column["@name"], column["#text"]])
print "item inserted \n"
产生以下错误
Traceback (most recent call last):
File "dbimport.py", line 12, in <module>
c.execute("INSERT INTO stocks ? VALUES '?'", [column["@name"], column["#text"]])
sqlite3.OperationalError: near "?": syntax error
我在这里做错了什么?我之前使用过这种方法,它工作得很好,虽然没有XML文件。
答案 0 :(得分:2)
?
只能用于值,而不能用于列名。此
INSERT INTO stocks ? VALUES '?'
无效。你必须使用
INSERT INTO stocks (columnname) VALUES (?)
请注意?
周围缺少的引号。在代码中:
c.execute("INSERT INTO stocks ({}) VALUES (?)".format(column["@name"]), column["#text"])