使用循环更新sqlite行

时间:2017-05-01 10:00:34

标签: python sqlite

我正在尝试根据某些条件更新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,) )

1 个答案:

答案 0 :(得分:0)

我必须使用

with sqlite3.connect("911.db",timeout=10, isolation_level=None) as connection:

添加isolation_level = None解决了建议here

的问题