如何将数据添加到SQLite中的列?
问题描述:
我已经重写我的“死囚”程序中加入一些改进,使之与Python 3如何将数据添加到SQLite中的列?
这是兼容它的外观现在:
import sqlite3
import re
import urllib.request
from bs4 import BeautifulSoup
import requests
import string
URL=[]
conn = sqlite3.connect('*.sqlite')
conn.text_factory = str
cur = conn.cursor()
cur.execute("DROP TABLE IF EXISTS *")
cur.execute("CREATE TABLE * (Execution text,Statement text, LastName text, Firstname text, TDCJNumber text, Age integer, Date text, Race text, County text)")
conn.commit()
url='http://www.tdcj.state.tx.us/death_row/dr_executed_offenders.html'
lines = urllib.request.urlopen(url)
*data = lines.read()
lines.close()
soup = BeautifulSoup(*data,"html.parser")
rows = soup.find_all('tr')
url2 = url[:38]
for row in rows:
td = row.find_all('td')
try:
Execution = str(td[0].get_text())
cur.execute("INSERT OR IGNORE INTO * (Execution) VALUES(?);", (str(Execution),))
lastname= str(td[3].get_text())
cur.execute("INSERT OR IGNORE INTO * (Execution) VALUES(?);", (str(Execution),))
firstname= str(td[4].get_text())
cur.execute("INSERT OR IGNORE INTO * (Firstname) VALUES(?);", (str(firstname),))
tdcj= str(td[5].get_text())
cur.execute("INSERT OR IGNORE INTO * (TDCJNumber) VALUES(?);", (str(tdcj),))
Age= str(td[6].get_text())
cur.execute("INSERT OR IGNORE INTO * (Age) VALUES(?);", (str(Age),))
Date= str(td[7].get_text())
cur.execute("INSERT OR IGNORE INTO * (Date) VALUES(?);", (str(Date),))
Race= str(td[8].get_text())
cur.execute("INSERT OR IGNORE INTO * (Race) VALUES(?);", (str(Race),))
County= str(td[9].get_text())
cur.execute("INSERT OR IGNORE INTO * (County) VALUES(?);", (str(County),))
links = row.find_all("a")
link = links[1].get("href")
LastStatementLink = url2 + link
lines2 = urllib.request.urlopen(LastStatementLink)
URL.append(LastStatementLink)
except Exception:
print ("An error has occured")
continue
for U in URL:
try:
r = requests.get(U)
r.raise_for_status()
print ("URL OK"), U
document = urllib.request.urlopen(U)
html = document.read()
soup = BeautifulSoup(html,"html.parser")
pattern = re.compile("Last Statement:")
Statement = soup.find(text=pattern).findNext('p').contents[0]
print (Statement.encode("utf-8"))
cur.execute("INSERT OR IGNORE INTO * (Statement) VALUES(?);", (str(Statement),))
continue
except requests.exceptions.HTTPError as err:
print (err)
conn.commit()
conn.close()
我一直在尝试了几个小时以获得正确的数据插入SQLite)数据库,但我似乎无法得到正确的。例如:
for row in rows:
td = row.find_all('td')
try:
Execution = str(td[0].get_text())
cur.execute("INSERT OR IGNORE INTO * (Execution) VALUES(?);", (str(Execution),))
该方案具有在规定的网站上找到该表中的所有执行数字在位置[0],并将其添加到该列的数据库“执行”它为所有这样做数据的其他部分也是如此。
问题是,python似乎创建一个新的行,每次它必须添加一块数据到数据库。该程序运行,但我有4000行,而不是我的SQLite数据库中约540。
我试过了很多东西,但我似乎无法将数据放入正确的列中,或者以正确数量的行结束。
我一直在想不同的方法:将数据添加到列表中,然后将它们添加到数据库中。这意味着我将有8个数据列表(每个执行编号,名字,姓氏,......的一个列表)但是,如何将数据从列表添加到SQLite?
谢谢!
答
问题是,python似乎创建一个新的行,每次它有 添加一块数据到数据库。该程序运行,但我有我的SQLite数据库中的4000行而不是约540。
啊哈,这不是蟒蛇这是这样做,但它是你。你有8个插入语句,你应该有一个
Execution = str(td[0].get_text())
cur.execute("INSERT OR IGNORE INTO * (Execution,Firstname, ..., Race,) VALUES(?,?,?,?,?,?,?);", (str(Execution) , str(Firstname), ...))
应该这样做。请注意统计所有?
请确保列出所有列名称,并且已添加所有这些列的值。
此外,请注意,惯例是变量应以小写字母开头。
您是否尝试更新满足某些条件的表中的现有行或插入新行? – Fejs
'INSERT'总是添加新行 - 学习'SQL'。 – furas