-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_utils.py
40 lines (36 loc) · 1.45 KB
/
db_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import sys
import os
import sqlite3
import json
from fastapi import HTTPException
if sys.platform == "win32":
storage_folder = os.path.join(os.getenv('APPDATA'),"DeepMake")
elif sys.platform == "darwin":
storage_folder = os.path.join(os.getenv('HOME'),"Library","Application Support","DeepMake")
elif sys.platform == "linux":
storage_folder = os.path.join(os.getenv('HOME'),".local", "DeepMake")
def store_data(key: str, item: dict):
conn = sqlite3.connect(os.path.join(storage_folder, 'data_storage.db'))
cursor = conn.cursor()
value = json.dumps(dict(item))
cursor.execute("REPLACE INTO key_value_store (key, value) VALUES (?, ?)", (key, value))
conn.commit()
conn.close()
return {"message": "Data stored successfully"}
def retrieve_data(key: str):
conn = sqlite3.connect(os.path.join(storage_folder, 'data_storage.db'))
cursor = conn.cursor()
cursor.execute("SELECT value FROM key_value_store WHERE key = ?", (key,))
row = cursor.fetchone()
conn.close()
if row:
data = json.loads(row[0])
return data
raise HTTPException(status_code=404, detail="Key not found")
def delete_data(key: str):
conn = sqlite3.connect(os.path.join(storage_folder, 'data_storage.db'))
cursor = conn.cursor()
cursor.execute("DELETE FROM key_value_store WHERE key = ?", (key,))
conn.commit()
conn.close()
return {"message": "Data deleted successfully"}