Python / SQLite3 - 将输出插入现有表

时间:2017-05-10 15:08:40

标签: python sqlite

好的团队,所以我很难根据自己的情况找到文档/答案,现在我只是在转动我的车轮。

def main():
    Scrape("c:\source\list2.csv", "General_Availability")

# Create data
def Scrape(Passed_Link, Release):
    conn = sqlite3.connect("C:/source/Matts.sqlite3")
    c = conn.cursor()
    with open(Passed_Link) as f:
      for row in csv.reader(f):
        for url in row:
          r = requests.get(url)
          soup = BeautifulSoup(r.content, 'lxml')
          tables = soup.find('table', attrs={"class": "hpui-standardHrGrid-table"})
          for rows in tables.find_all('tr', {'releasetype': Release})[0:1]:
            item = []
            for val in rows.find_all('td')[0:1]:
              item.append(val.text.strip())
          prurl = url[-6:]
          rows = [prurl, item, "Placeholder"]
          print (rows)
    for sqlrow in item:
        c.execute('INSERT INTO APP_devicefirmware VALUES (?)', rows)

main()    

所以当我这样做时,我收到一个错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in main
  File "<stdin>", line 19, in Scrape
  sqlite3.OperationalError: table APP_devicefirmware has 3 columns but 1 values were supplied`

1 个答案:

答案 0 :(得分:1)

rows是一个值。您必须提供列表rows中存储的每个值,如下所示:

c.execute('INSERT INTO APP_devicefirmware VALUES (?, ?, ?);', (rows[0], rows[1], rows[2])