2
我想解析XML文件並將其導入到SQLITE數據庫中。Python導入XML介紹SQLITE(xmltodict)
的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文件。
謝謝。該查詢現在可以工作,但不會將任何內容插入到數據庫中。有趣... – Faryus
你最後一次插入後提交嗎? –
是的,我意識到我沒有犯。現在我必須調整我的代碼,以便將所有內容插入到一行中。目前它將每個值插入到它自己的行中。我需要在循環中升級,大概:) http://imgr.ro/images/ab8b8cf5c0.png – Faryus