WeChatMsg/app/DataBase/micro_msg.py

76 lines
1.8 KiB
Python
Raw Normal View History

2023-11-15 21:57:29 +08:00
import os.path
2023-11-12 21:51:33 +08:00
import sqlite3
2023-11-18 11:51:58 +08:00
import threading
2023-11-12 21:51:33 +08:00
2023-11-18 11:51:58 +08:00
lock = threading.Lock()
2023-11-15 21:57:29 +08:00
DB = None
cursor = None
db_path = "./app/Database/Msg/MicroMsg.db"
2023-11-15 21:57:29 +08:00
def singleton(cls):
_instance = {}
def inner():
if cls not in _instance:
_instance[cls] = cls()
return _instance[cls]
return inner
2023-11-16 00:13:49 +08:00
2023-11-15 21:57:29 +08:00
def is_database_exist():
return os.path.exists(db_path)
@singleton
class MicroMsg:
def __init__(self):
self.DB = None
self.cursor = None
self.open_flag = False
self.init_database()
2023-11-12 21:51:33 +08:00
def init_database(self):
if not self.open_flag:
if os.path.exists(db_path):
self.DB = sqlite3.connect(db_path, check_same_thread=False)
# '''创建游标'''
self.cursor = self.DB.cursor()
self.open_flag = True
if lock.locked():
lock.release()
2023-11-12 21:51:33 +08:00
def get_contact(self):
if not self.open_flag:
return None
try:
lock.acquire(True)
sql = '''select UserName,Alias,Type,Remark,NickName,PYInitial,RemarkPYInitial,ContactHeadImgUrl.smallHeadImgUrl,ContactHeadImgUrl.bigHeadImgUrl
from Contact inner join ContactHeadImgUrl on Contact.UserName = ContactHeadImgUrl.usrName
where Type%2=1 and Alias is not null
order by PYInitial
'''
self.cursor.execute(sql)
result = self.cursor.fetchall()
finally:
lock.release()
return result
2023-11-12 21:51:33 +08:00
def close(self):
if self.open_flag:
try:
lock.acquire(True)
self.open_flag = False
self.DB.close()
finally:
lock.release()
2023-11-12 21:51:33 +08:00
def __del__(self):
self.close()
2023-11-16 23:16:38 +08:00
2023-11-12 21:51:33 +08:00
if __name__ == '__main__':
pass
# get_contact()