python библиотека для упращения работы с базой

Всем привет, посоветуйте библиотеки/классы для упрощения работы с базой.

Я реализовал свой класс для работы с базой, но т.к я новичок в python что-то мне подсказывает что это говнокод, в нем несколько проблем: во-первых постоянное повторение одного и того же кода, а также похоже соединение с базой не закрывается и появляются проблемы с памятью.


class Database:
    db = None

    def __init__(self, dbPath):
        self.dbPath = dbPath

    @property
    def connect(self):
        if self.db == None:
           self.db = sqlite3.connect(self.dbPath) 
        return self.db

    @connect.deleter
    def connect(self):
        print('Close')
        self.db.close()

    def create_db(self): 
        """ if os.path.exists(self.dbPath) == True:
            return     """
        
        cursor = self.connect.cursor()

        cursor.execute("""CREATE TABLE IF NOT EXISTS temp_notify (
            notify_id INTEGER,
            user_id INTEGER,
            ex_date INTEGER
        )""") 

        cursor.execute("""CREATE TABLE IF NOT EXISTS temp_search (
            user_id INTEGER,
            status,
            ex_date INTEGER
        )""") 
 
        self.connect.commit()    
        cursor.close()  

    
    def addNotify(self, notify_id, user_id):
        cursor = self.connect.cursor()
        cursor.execute(f"INSERT INTO temp_notify (notify_id, user_id, ex_date) VALUES({notify_id}, {user_id}, {int(time.time())+(20 * 60 * 60)});")
        self.connect.commit()  
        cursor.close()   

    def getNotify(self, notify_id, user_id):
        cursor = self.connect.cursor() 
        q = cursor.execute(f"SELECT notify_id from temp_notify where notify_id = {notify_id} and user_id = {user_id}").fetchone() 
        self.connect.commit()  
        cursor.close()
        return q  

    def getSearch(self, user_id):
        cursor = self.connect.cursor() 
        q = cursor.execute(f"SELECT user_id from temp_search where user_id = {user_id}").fetchone() 
        self.connect.commit()  
        cursor.close()
        return q   

    def addSearch(self, user_id):
        cursor = self.connect.cursor()
        cursor.execute(f"INSERT INTO temp_search (user_id,status, ex_date) VALUES({user_id}, 0, {int(time.time())+(9 * 60 * 60)});")
        self.connect.commit()  
        cursor.close()     

    def updateSearch(self, user_id, date):
        cursor = self.connect.cursor()
        cursor.execute(f"UPDATE temp_search SET status = 1, ex_date = {date} where user_id = {user_id} ")  
        self.connect.commit()  
        cursor.close()   

Есть ли какие-то подобные библиотеки, как мой класс, чтобы не городить свой велосипед и быть уверенным в его правильной работе?


Ответы (0 шт):