【问题标题】:update sqlite rows using loops使用循环更新 sqlite 行
【发布时间】: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,) )

【问题讨论】:

  • 更新查询后你忘了提交
  • 你不能修改你用游标迭代的数据。

标签: python sqlite


【解决方案1】:

我不得不使用

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

添加isolation_level=None 解决了建议here 的问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-06
    • 2014-12-07
    • 1970-01-01
    • 1970-01-01
    • 2018-12-06
    • 2016-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多