mugunthjhs commited on
Commit
441e531
·
verified ·
1 Parent(s): 59a466a

Update database.py

Browse files
Files changed (1) hide show
  1. database.py +30 -28
database.py CHANGED
@@ -1,39 +1,41 @@
1
  import sqlite3
2
- import os
3
  from langchain_community.utilities.sql_database import SQLDatabase
 
4
 
5
- APP_DIR = "/app"
6
- DB_FILENAME = os.path.join(APP_DIR, "tasks.db")
7
  DB_PATH_URI = f"sqlite:///{DB_FILENAME}"
8
 
9
-
10
  def create_db_and_table():
11
- """Creates the SQLite database and tasks table if they don't exist."""
12
-
13
- conn = sqlite3.connect(DB_FILENAME) # Uses the absolute path
14
- cursor = conn.cursor()
15
- cursor.execute("""
16
- CREATE TABLE IF NOT EXISTS tasks (
17
- id INTEGER PRIMARY KEY AUTOINCREMENT,
18
- user_name TEXT,
19
- user_email TEXT,
20
- task_name TEXT NOT NULL,
21
- status TEXT DEFAULT 'pending',
22
- category TEXT,
23
- created_at TEXT DEFAULT (strftime('%Y-%m-%d %H:%M:%S', 'now', 'localtime')),
24
- due_date TEXT,
25
- due_time TEXT
26
- )
27
- """)
28
- try:
29
  cursor.execute("""
30
- CREATE UNIQUE INDEX IF NOT EXISTS idx_unq_user_task
31
- ON tasks (user_email, task_name, due_date, COALESCE(due_time, ''));
 
 
 
 
 
 
 
 
 
32
  """)
33
- except sqlite3.OperationalError as e:
34
- print(f"Warning: Could not create unique index idx_unq_user_task, it might exist or conflict: {e}")
35
- conn.commit()
36
- conn.close()
 
 
 
 
 
 
 
 
 
 
37
 
38
  def get_db_info():
39
  db = SQLDatabase.from_uri(DB_PATH_URI, include_tables=['tasks'])
 
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
+ if not os.path.exists(DB_FILENAME):
10
+ conn = sqlite3.connect(DB_FILENAME)
11
+ cursor = conn.cursor()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  cursor.execute("""
13
+ CREATE TABLE IF NOT EXISTS tasks (
14
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
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
+ try:
26
+ cursor.execute("""
27
+ CREATE UNIQUE INDEX IF NOT EXISTS idx_unq_user_task
28
+ ON tasks (user_email, task_name, due_date, COALESCE(due_time, ''));
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'])