add db path

This commit is contained in:
2025-09-17 10:39:58 +03:00
parent b87569bed6
commit 384c6f1c8c
3 changed files with 21 additions and 11 deletions

View File

@@ -1,12 +1,16 @@
import sqlite3
import os
class DBApplications:
def __init__(self):
self.db_path = 'instance/dev.db'
# Allow DB path override via env, default to /app/instance/dev.db inside container
default_path = os.getenv("DB_PATH", "instance/dev.db")
self.db_path = os.path.abspath(default_path)
os.makedirs(os.path.dirname(self.db_path), exist_ok=True)
self.create_table()
def create_table(self):
with sqlite3.connect(self.db_path) as conn:
with sqlite3.connect(self.db_path, check_same_thread=False) as conn:
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS applications (
@@ -23,7 +27,7 @@ class DBApplications:
def insert_application(self, user_id, name, access_code: str) -> bool:
try:
with sqlite3.connect(self.db_path) as conn:
with sqlite3.connect(self.db_path, check_same_thread=False) as conn:
cursor = conn.cursor()
cursor.execute("""
INSERT INTO applications (user_id, name, access_code)
@@ -35,7 +39,7 @@ class DBApplications:
return False
def get_application_by_id(self, id):
with sqlite3.connect(self.db_path) as conn:
with sqlite3.connect(self.db_path, check_same_thread=False) as conn:
cursor = conn.cursor()
cursor.execute("""
SELECT * FROM applications
@@ -55,7 +59,7 @@ class DBApplications:
return None
def get_applications(self, user_id):
with sqlite3.connect(self.db_path) as conn:
with sqlite3.connect(self.db_path, check_same_thread=False) as conn:
cursor = conn.cursor()
cursor.execute("""
SELECT * FROM applications
@@ -79,7 +83,7 @@ class DBApplications:
return all_rows
def delete(self, id):
with sqlite3.connect(self.db_path) as conn:
with sqlite3.connect(self.db_path, check_same_thread=False) as conn:
cursor = conn.cursor()
cursor.execute("""
DELETE FROM applications WHERE id = ?

View File

@@ -1,12 +1,16 @@
import sqlite3
import os
class Users:
def __init__(self):
self.db_path = 'instance/dev.db'
# Allow DB path override via env, default to /app/instance/dev.db inside container
default_path = os.getenv("DB_PATH", "instance/dev.db")
self.db_path = os.path.abspath(default_path)
os.makedirs(os.path.dirname(self.db_path), exist_ok=True)
self.create_table()
def create_table(self):
with sqlite3.connect(self.db_path) as conn:
with sqlite3.connect(self.db_path, check_same_thread=False) as conn:
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS users (
@@ -22,7 +26,7 @@ class Users:
def insert_user(self, email, password: str) -> bool:
try:
with sqlite3.connect(self.db_path) as conn:
with sqlite3.connect(self.db_path, check_same_thread=False) as conn:
cursor = conn.cursor()
cursor.execute("""
INSERT INTO users (email, password)
@@ -34,7 +38,7 @@ class Users:
return False
def get_user_by_id(self, id):
with sqlite3.connect(self.db_path) as conn:
with sqlite3.connect(self.db_path, check_same_thread=False) as conn:
cursor = conn.cursor()
cursor.execute("""
SELECT * FROM users
@@ -53,7 +57,7 @@ class Users:
return None
def get_user_by_email(self, email):
with sqlite3.connect(self.db_path) as conn:
with sqlite3.connect(self.db_path, check_same_thread=False) as conn:
cursor = conn.cursor()
cursor.execute("""
SELECT * FROM users