Skip to content
This repository has been archived by the owner on Nov 10, 2023. It is now read-only.

Commit

Permalink
implement thread lock to avoid recursive use of cursor (sqlite3.Progr…
Browse files Browse the repository at this point in the history
…ammingError)
  • Loading branch information
thearyadev committed Jan 5, 2023
1 parent 1766626 commit e81f9ee
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion database/database_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,29 @@ def __init__(self, db_path: str = "./accounts.sqlite"):
super().__init__(db_path)

def insert(self, account: util.MicrosoftAccount):
lock.acquire()
self.cursor.execute(
"""
INSERT INTO MicrosoftAccount (email, password, lastExec, points) VALUES (?, ?, ?, ?)
""",
(account.email, account.password, account.lastExec, account.points)
)
self.connection.commit()
lock.release()

def read(self) -> list[util.MicrosoftAccount]:
lock.acquire()
self.cursor.execute(
"""
SELECT * FROM MicrosoftAccount
"""
)
return [util.MicrosoftAccount(**data) for data in self.cursor.fetchall()]
data = [util.MicrosoftAccount(**data) for data in self.cursor.fetchall()]
lock.release()
return data

def write(self, account: util.MicrosoftAccount):
lock.acquire()
self.cursor.execute(
"""
UPDATE MicrosoftAccount
Expand All @@ -37,8 +43,10 @@ def write(self, account: util.MicrosoftAccount):
(account.email, account.password, account.lastExec, account.points, account.id)
)
self.connection.commit()
lock.release()

def delete(self, account: util.MicrosoftAccount):
lock.acquire()
self.cursor.execute(
"""
DELETE FROM MicrosoftAccount
Expand All @@ -47,3 +55,4 @@ def delete(self, account: util.MicrosoftAccount):
(account.id,)
)
self.connection.commit()
lock.release()

0 comments on commit e81f9ee

Please sign in to comment.