-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect_db.py
38 lines (30 loc) · 1.02 KB
/
connect_db.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
import sqlite3
import json
import os
class ConnectDB:
def __init__(self):
self.chat_db_path = "datas/data.json"
def get_chat_data(self):
chat_db = []
if os.path.exists(self.chat_db_path) and os.path.getsize(self.chat_db_path) > 0:
with open(self.chat_db_path, "r") as f:
chat_db = json.load(f)
return chat_db
def get_chat_title_list(self):
chat_list = []
chat_db = self.get_chat_data()
for chat in chat_db:
title = chat.get("title")
chat_list.append(title)
return chat_list
def save_chat_data(self, new_chat_data):
with open(self.chat_db_path, "w") as f:
f.write(json.dumps(new_chat_data))
def delete_all_data(self):
chat_db = self.get_chat_data()
chat_db.clear()
self.save_chat_data(chat_db)
def delete_chat_data(self, index):
chat_db = self.get_chat_data()
chat_db.pop(index)
self.save_chat_data(chat_db)