简体   繁体   English

sqlite3.OperationalError:无法打开数据库文件 - 一段时间后发生错误

[英]sqlite3.OperationalError: unable to open database file - error occurs after a certain while

Tooling:工具:

  • Raspberry Pi 3B树莓派 3B
  • Raspbian树莓派
  • BME280 BME280
  • Python3蟒蛇3
  • Flask烧瓶
  • Sqlite3 Sqlite3

Error code:错误代码:

Traceback (most recent call last):
  File "BME280_DataCollector.py", line 65, in <module>
  File "BME280_DataCollector.py", line 45, in logData
sqlite3.OperationalError: unable to open database file

Working on Raspbian and want to store my sensor data in sqlite3 database.在 Raspbian 上工作并希望将我的传感器数据存储在 sqlite3 数据库中。 Somehow following error code occurs: "sqlite3.OperationalError: unable to open database file".不知何故出现以下错误代码:“sqlite3.OperationalError:无法打开数据库文件”。

Firstly, I thought that I requested the database file too quickly and changed the the measurement time to minutes, but the error is still reproducible.首先,我认为我请求数据库文件太快,将测量时间更改为分钟,但错误仍然可以重现。

I looked into /tmp by df /tmp.我通过 df /tmp 查看了 /tmp。 But this file system is used by 12 % and not overloaded.但是这个文件系统被 12% 使用并且没有过载。

Also, I tried to give the full path and also the database all write and read permissions via chmod, but also no differents.此外,我尝试通过 chmod 提供完整路径以及数据库的所有写入和读取权限,但也没有什么不同。 In addition, I put the full path to the code.另外,我把代码的完整路径。

Furthermore, I tried to make try and exception approaches which also weren't fruitful.此外,我尝试尝试和异常方法,但也没有成果。

Nevertheless, I wanted to know if this failure occures at a certain number of interactions with the database.尽管如此,我想知道这种失败是否发生在与数据库进行一定数量的交互时。 I found out that it always stopped at the 1020th interaction.我发现它总是在第 1020 次交互时停止。

I also tried to restart the python script with a shell script but it didn't work out due to lack of experience and knowledge.我还尝试使用 shell 脚本重新启动 python 脚本,但由于缺乏经验和知识,它没有成功。

Code:代码:


from flask import Flask,render_template,url_for,request,redirect, make_response, send_file
import random
import json
from time import sleep
from random import random
from flask import Flask, render_template, make_response
import datetime

import sqlite3
import sys

from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
import io
import os

import smbus2
import bme280


## FUNCTIONS ##
# get data
def getBMEdata():
    port = 1
    adress = 0x77
    bus = smbus2.SMBus(port)
    calibration_params = bme280.load_calibration_params(bus, adress)
        
    bme_data = bme280.sample(bus, adress, calibration_params)
    temp = '%.2f' % bme_data.temperature
    hum = '%.2f' % bme_data.humidity
    press = '%.2f' % bme_data.pressure
    
    now = datetime.datetime.now() #get time
    timeString = now.strftime('%d-%m-%Y %H:%M:%S') #write time to string
        
    return temp, hum, press, timeString

# function to insert data on a table
def logData(temp, hum, press, timeString):
    conn = sqlite3.connect(dbname)
    curs = conn.cursor()
    curs.execute("INSERT INTO BME280_data values((?), (?), (?), (?))", (timeString, temp, hum, press))
    conn.commit()
    conn.close()
        
# display data base
def displayData():
    conn = sqlite3.connect(dbname)
    curs = conn.cursor()
    print("\nEntire database contents:\n")
    for row in curs.execute("SELECT * FROM BME280_data"):
        print(row)
    conn.close()
    
## MAIN
if __name__ == '__main__':
    count = 0
    dbname = '/home/pi/projects/SmartPlanting/Sensors_Database/sensorsData.db'
    sampleFreq = 60 #data collect every minute
    while True:
        temp, hum, press, timeString = getBMEdata() #get data
        logData(temp, hum, press, timeString) #save data
        sleep(sampleFreq) #wait
        displayData() #show in terminal
        #count = count+1
        #print(count)

Maybe someone already solved this problem or can give me an alternative to sqlite3 which works with flask.也许有人已经解决了这个问题,或者可以给我一个与flask一起使用的sqlite3的替代方案。

Suggestion: add a more complete exception handling routine, because your stacktrace could be more verbose.建议:添加更完整的异常处理例程,因为您的堆栈跟踪可能更冗长。

But judging from your trace the offending line #45 could be this: conn.commit() (or the line above).但是从您的跟踪来看,有问题的第 45 行可能是这样的: conn.commit() (或上面的行)。 Python is already helping you pinpoint the error. Python 已经在帮助您查明错误。 There is something wrong in function logData.函数 logData 有问题。

Could it be that you are feeding incorrect data to your table BME280_data ?可能是您向表 BME280_data 提供了不正确的数据? To debug your application I would strongly recommend that you print log the data you are trying to insert (use the logging module to output to file and/or console).要调试您的应用程序,我强烈建议您打印日志您尝试插入的数据(使用日志记录模块输出到文件和/或控制台)。 I don't know the structure of your table but some of your fields could have a definition (data type) that is not compatible with the data you are trying to insert.我不知道您的表的结构,但您的某些字段的定义(数据类型)可能与您尝试插入的数据不兼容。 The fact that you are able to predictably reproduce the problem is quite telling and my hunch is that the data could be the cause.您能够以可预测的方式重现问题这一事实很有说服力,我的直觉是数据可能是原因。

To sum up: take good habits now and add at least basic exception handling.总结一下:现在就养成良好的习惯,至少添加基本的异常处理。 A quality application should have exception handling and log errors so they can be reviewed and remediated by a human.一个质量应用程序应该有异常处理和日志错误,以便他们可以被人工审查和纠正。 This is even more important for unattended applications, because you are not in front of the console and you may not even have a chance to see problems that occur.这对于无人值守的应用程序更为重要,因为您不在控制台前,您甚至可能没有机会看到发生的问题。

Here is one tutorial that may help: https://code.tutsplus.com/tutorials/error-handling-logging-in-python--cms-27932这是一个可能有帮助的教程: https : //code.tutsplus.com/tutorials/error-handling-logging-in-python--cms-27932

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 仅在 PC 启动 sqlite3.OperationalError:无法打开数据库文件时出现 Python 错误 - Python Error just on PC startup sqlite3.OperationalError: unable to open database file 当带有 wsgi 脚本 [sqlite3.OperationalError) 的带有聊天机器人的烧瓶应用程序无法打开数据库文件时出错] - error when flask app with chatter bot hosted with a wsgi script [sqlite3.OperationalError) unable to open database file] sqlite3.OperationalError:“,”附近:语法错误python - sqlite3.OperationalError: near “,”: syntax error python sqlite3.OperationalError:接近“%”:语法错误? - sqlite3.OperationalError: near “%”: syntax error? sqlite3.OperationalError:“ ProductID”附近:语法错误 - sqlite3.OperationalError: near “ProductID”: syntax error 气流操作错误 sqlalquemy (sqlite3.OperationalError) - Airflow Operational error sqlalquemy (sqlite3.OperationalError) sqlite3.OperationalError:“/”附近:语法错误 - sqlite3.OperationalError: near “/”: syntax error sqlite3.OperationalError:靠近“show”:语法错误 - sqlite3.OperationalError: near "show": syntax error sqlite3.OperationalError:“”附近:语法错误 - sqlite3.OperationalError: near “ ”: syntax error sqlite3.OperationalError:靠近“。”:语法错误 - sqlite3.OperationalError: near “.”: syntax error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM