I made an app which uses sqllite3 database. When I run the .py file it works just fine.
Here is the code for the database:
import sqlite3
connection = sqlite3.connect("game.db")
connection.execute("PRAGMA foreign_keys = 1;")
cursor = connection.cursor()
def run(sql):
cursor.execute(sql)
connection.commit()
return [i for i in cursor]
run("CREATE TABLE IF NOT EXISTS accounts (playerName TEXT PRIMARY KEY, playerPassword TEXT);")
run("CREATE TABLE IF NOT EXISTS scores (playerName REFERENCES accounts(playerName), difficulty TEXT, score INTEGER);")
I froze this program using cx_Freeze.
This is the setup.py:
import cx_Freeze
import pkgutil
from os.path import join as path_join
def get_all_packages():
return [i.name for i in list(pkgutil.iter_modules()) if i.ispkg] # Return name of all package
# base = "Win32GUI" allows your application to open without a console window
executables = [cx_Freeze.Executable('main.py', base="Win32GUI",
target_name="Push-Ups Game", icon=path_join("Images", "Push-Ups Game Logo.ico"))]
packages = ["collections", "encodings", "importlib", "pygame", "cv2", "mediapipe",
"math", "sqlite3", "random", "numpy", "time", "os", "re", "ctypes",
"matplotlib", "logging", "urllib", "packaging", "PIL", "pyparsing",
"html", "cycler", "dateutil", "kiwisolver", "json", "xml", "http", "attr", "sys"]
cx_Freeze.setup(
name="Game",
options={"build_exe":
{"includes": packages,
"excludes": [i for i in get_all_packages() if i not in packages],
"include_files": ["Music/", "Images/", "game.db"]}},
executables=executables
)
When I run the .exe file, weirdly the database sometimes just works fine, but occasionally it gives me the following error:
sqllite3.OperationError: Unable to open database file
I thought maybe the problem was with creating a .db file, so I removed it and ran the .exe and it generated the .db without a problem. I ran it with the .db already, also worked fine. It occasionally does not work and I could not find any correlation.
I saw a bunch of questions about this very same error, but I could not find any which had the same problem of the code sometimes working and sometimes not.
Any help is appreciated 🙂