加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – sqlite3数据库被锁定

发布时间:2020-12-20 13:52:41 所属栏目:Python 来源:网络整理
导读:我必须从操作表中删除记录时出现数据库锁定错误. 在sqlite3数据库上有两个读写程序 一个是在sqlite3表上写入硬件操作结果的c程序,另一个是从sqlite读取记录并在完成作业后处理它们并删除行的python脚本. 但删除行时python脚本show database被锁定错误.. db n
我必须从操作表中删除记录时出现数据库锁定错误.

在sqlite3数据库上有两个读写程序

一个是在sqlite3表上写入硬件操作结果的c程序,另一个是从sqlite读取记录并在完成作业后处理它们并删除行的python脚本.

但删除行时python脚本show database被锁定错误..

db name:db.db

数据库表:
表’行动'(
????‘摆脱’INTEGER PRIMARY KEY AUTOINCREMENT,
????‘所有者’INTEGER,
????‘行动’文字,
????‘node’TEXT,
????‘价值’文字

python脚本:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sqlite3
import time
import os.path
import requests
#import urllib.parse

#defines
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
db_path = os.path.join(BASE_DIR+"/dbs/","db.db")
wd_file_path = os.path.join(BASE_DIR,"wd")
pid = os.getpid()
conn = sqlite3.connect(db_path,isolation_level=None,timeout=30000)
print ("Opened database successfully");
while True:
    res = conn.execute("select * from 'actions' where 'owner'='1';")
    #conn.commit()
    data=res.fetchone()
    print(data)
    if (data is None) :
        print('nothing @ '+str(time.time()))
        with open(wd_file_path,'w') as file_:
            file_.write("{'pid'='"+str(pid)+"','time'='"+str(time.time())+"'}")
        time.sleep(0.5)
    else:
        #print(data)
        r = requests.post("http://127.0.0.1/json.php",data={'act': data[2],'val': data[4]})
        #if (r.text == '1'):
        conn.execute("delete from 'actions' where 'rid'="+str(data[0])+";")
        conn.commit()
        #else:
        #   print(r.text)

你可以看到我在我的连接上放了isolation_level = None和timeout = 30000
但我多次得到数据库锁定错误

解决方法

考虑删除无限时True循环并使用 connection cursor执行和获取语句:

conn = sqlite3.connect(db_path,timeout=30000) 
print("Opened database successfully")

cur = conn.cursor()
cur.execute("select * from 'actions' where 'owner'='1';") 

for data in cur.fetchall() 
  print(data) 

  if (data is None): 
    print('nothing @ '+str(time.time())) 
    with open(wd_file_path,'w') as file_: 
      file_.write("{'pid'='"+str(pid)+"','time'='"+str(time.time())+"'}") 
    time.sleep(0.5) 
  else: 
    #print(data) 
    r = requests.post("http://127.0.0.1/json.php",'val': data[4]}) 
    #if (r.text == '1'): 
    cur.execute("delete from 'actions' where 'rid'="+str(data[0])+";") 
    conn.commit() 
    #else: 
    # print(r.text)

cur.close()
conn.close()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读