Spaces:
Sleeping
Sleeping
Update database.py
Browse files- database.py +23 -30
database.py
CHANGED
|
@@ -1,41 +1,34 @@
|
|
| 1 |
import sqlite3
|
| 2 |
from langchain_community.utilities.sql_database import SQLDatabase
|
| 3 |
-
import os
|
| 4 |
|
| 5 |
DB_FILENAME = "tasks.db"
|
| 6 |
DB_PATH_URI = f"sqlite:///{DB_FILENAME}"
|
| 7 |
|
| 8 |
def create_db_and_table():
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
cursor.execute("""
|
| 13 |
-
CREATE
|
| 14 |
-
|
| 15 |
-
user_name TEXT,
|
| 16 |
-
user_email TEXT,
|
| 17 |
-
task_name TEXT NOT NULL,
|
| 18 |
-
status TEXT DEFAULT 'pending',
|
| 19 |
-
category TEXT,
|
| 20 |
-
created_at TEXT DEFAULT (strftime('%Y-%m-%d %H:%M:%S', 'now', 'localtime')),
|
| 21 |
-
due_date TEXT,
|
| 22 |
-
due_time TEXT
|
| 23 |
-
)
|
| 24 |
""")
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
""")
|
| 30 |
-
except sqlite3.OperationalError as e:
|
| 31 |
-
print(f"Warning: Could not create unique index idx_unq_user_task, it might exist or conflict: {e}")
|
| 32 |
-
conn.commit()
|
| 33 |
-
conn.close()
|
| 34 |
-
print("Database and table created.")
|
| 35 |
-
else:
|
| 36 |
-
print("Database already exists, connecting to the existing database.")
|
| 37 |
-
|
| 38 |
-
|
| 39 |
|
| 40 |
def get_db_info():
|
| 41 |
db = SQLDatabase.from_uri(DB_PATH_URI, include_tables=['tasks'])
|
|
@@ -99,4 +92,4 @@ def execute_dml_query(query: str, params: tuple = None):
|
|
| 99 |
print(f"General SQLite error executing DML query: {query}\n{e}")
|
| 100 |
raise
|
| 101 |
finally:
|
| 102 |
-
conn.close()
|
|
|
|
| 1 |
import sqlite3
|
| 2 |
from langchain_community.utilities.sql_database import SQLDatabase
|
|
|
|
| 3 |
|
| 4 |
DB_FILENAME = "tasks.db"
|
| 5 |
DB_PATH_URI = f"sqlite:///{DB_FILENAME}"
|
| 6 |
|
| 7 |
def create_db_and_table():
|
| 8 |
+
conn = sqlite3.connect(DB_FILENAME)
|
| 9 |
+
cursor = conn.cursor()
|
| 10 |
+
cursor.execute("""
|
| 11 |
+
CREATE TABLE IF NOT EXISTS tasks (
|
| 12 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 13 |
+
user_name TEXT,
|
| 14 |
+
user_email TEXT,
|
| 15 |
+
task_name TEXT NOT NULL,
|
| 16 |
+
status TEXT DEFAULT 'pending',
|
| 17 |
+
category TEXT,
|
| 18 |
+
created_at TEXT DEFAULT (strftime('%Y-%m-%d %H:%M:%S', 'now', 'localtime')),
|
| 19 |
+
due_date TEXT,
|
| 20 |
+
due_time TEXT
|
| 21 |
+
)
|
| 22 |
+
""")
|
| 23 |
+
try:
|
| 24 |
cursor.execute("""
|
| 25 |
+
CREATE UNIQUE INDEX IF NOT EXISTS idx_unq_user_task
|
| 26 |
+
ON tasks (user_email, task_name, due_date, COALESCE(due_time, ''));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
""")
|
| 28 |
+
except sqlite3.OperationalError as e:
|
| 29 |
+
print(f"Warning: Could not create unique index idx_unq_user_task, it might exist or conflict: {e}")
|
| 30 |
+
conn.commit()
|
| 31 |
+
conn.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
def get_db_info():
|
| 34 |
db = SQLDatabase.from_uri(DB_PATH_URI, include_tables=['tasks'])
|
|
|
|
| 92 |
print(f"General SQLite error executing DML query: {query}\n{e}")
|
| 93 |
raise
|
| 94 |
finally:
|
| 95 |
+
conn.close()
|