【发布时间】:2017-05-01 18:17:39
【问题描述】:
我正在尝试根据特定条件更新 Sqlite 数据库中的行。 我的代码没有返回任何错误,但我的 sqlite 行没有被更新。
import requests
from bs4 import BeautifulSoup
import sqlite3
import datetime
today = datetime.date.today()
with sqlite3.connect("911.db",timeout=10) as connection:
c = connection.cursor()
#go and fetch all links in DB
c.execute("SELECT url FROM Links")
#for each link I am going to see if it contains the div class "flashmessage warning"
for link in c:
page = requests.get(link[0])
soup = BeautifulSoup(page.content, 'html.parser')
status = soup.find_all('div', class_='flashmessage warning')
#check if list is not empty. If it is not then update value for update_row
if not status:
update_row = link[0]
d = connection.cursor()
d.execute("UPDATE Links SET status = 0 WHERE url == (?)", (update_row,) )
d.execute("UPDATE Links SET last_seen = (?) WHERE url == (?)", (today, update_row) )
#if list is not empty then update status with 1
else:
update_row = link[0]
e = connection.cursor()
e.execute("UPDATE Links SET status = 1 WHERE url == (?)", (update_row,) )
【问题讨论】:
-
更新查询后你忘了提交
-
你不能修改你用游标迭代的数据。