我试图从URL中提取一些XML,解析它并将条目存储在sqlite3数据库中,我尝试了很多东西而且都失败了。 Codde到目前为止:
#!/usr/bin/env python
from urllib2 import urlopen
import gc
import xml.etree.ElementTree as ET
import sqlite3
rosetta_url = ("https://boinc.bakerlab.org/rosetta/team_email_list.php?teamid=12575&account_key=Y&xml=1")
root = ET.parse(urlopen(rosetta_url)).getroot()
cpids = [el.text for el in root.findall('.//user/cpid')]
print cpids
conn = sqlite3.connect("GridcoinTeam.db")
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS GRIDCOINTEAM (cpid TEXT)''')
c.executemany("INSERT INTO GRIDCOINTEAM VALUES (?);", cpids)
conn.commit()
conn.close()
conn = sqlite3.connect("GridcoinTeam.db")
c = conn.cursor()
cpids = c.execute('select cpid from GRIDCOINTEAM').fetchall()
conn.close()
print cpids
gc.collect()
我得到错误:
Incorrect number of bindings supplied. The current statement uses 1, and there are 32 supplied.
我尝试通过更改为
来制作插入元组c.executemany("INSERT INTO GRIDCOINTEAM VALUES (?);", (cpids, ))
但这只是给出了:
Incorrect number of bindings supplied. The current statement uses 1, and there are 3289 supplied.
XML摘录的格式为[' 5da243d1f47b7852d372c51d6ee660d7' 5a6d18b942518aca60833401e70b75b1' 527ab53f75164864b74a89f3db6986b8'],但有几千个条目。
感谢。
答案 0 :(得分:1)
您需要将其作为多行而不是多列插入一行
cpids = [el.text for el in root.findall('.//user/cpid')]
cpids = zip(*[iter(cpids)]*1)
print cpids
答案 1 :(得分:0)
问题在于
c.executemany("INSERT INTO GRIDCOINTEAM VALUES (?);", cpids)
这个executemany
需要一个元组列表,但是你传递一个字符串列表。代码的有效作用是:
for entry in cpids:
c.execute("INSERT INTO GRIDCOINTEAM VALUES (?);", *entry)
请注意entry
之前的星号,它会卸载字符串,并且会给你32个参数,而你只需要一个。
为了解决这个问题,我们需要了解您的GRIDCOINTEAM
表架构。如果你有一个只有一列的表,你想插入它,你可能会这样做:
for entry in cpids:
c.execute("INSERT INTO GRIDCOINTEAM VALUES (?)", entry)
与executemany
相比,execute
将每个参数作为一个参数 - 没有元组,并在此处列出卸载。
或者你可以使用executemany
,但是你需要将每个字符串包装在一个元组或生成器中:
c.executemany("INSERT INTO GRIDCOINTEAM VALUES (?);", [(i,) for i in cpids])