replace old db connections

This commit is contained in:
2021-06-15 17:27:47 +02:00
parent c9ac507ab5
commit e49d0c3ab5

View File

@@ -27,7 +27,6 @@ class TimeTrack:
self.CATEGORIES = False self.CATEGORIES = False
self.REFERENCES = False self.REFERENCES = False
self.LOGFILE = 'timetrack.log' self.LOGFILE = 'timetrack.log'
self.DBCON = sqlite3.connect(self.DATABASE, detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES)
logging.basicConfig( logging.basicConfig(
filename=self.LOGFILE, filename=self.LOGFILE,
level=logging.DEBUG, level=logging.DEBUG,
@@ -37,9 +36,6 @@ class TimeTrack:
self.db_setup() self.db_setup()
self.load_config() self.load_config()
def __del__(self):
self.DBCON.close()
## Prepartation ## Prepartation
#============== #==============
### Check OS and clear screen ### Check OS and clear screen
@@ -146,7 +142,7 @@ class TimeTrack:
logging.debug('Create initial database tables') logging.debug('Create initial database tables')
try: try:
with self.DBCON as con: with sqlite3.connect(self.DATABASE, detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES) as con:
for SQL in sql: for SQL in sql:
con.execute(SQL) con.execute(SQL)
except sqlite3.Error as err: except sqlite3.Error as err:
@@ -290,8 +286,11 @@ class TimeTrack:
logging.debug('Accepted username: {}'.format(username)) logging.debug('Accepted username: {}'.format(username))
sql = "INSERT INTO users ( name ) values ( ? )" sql = "INSERT INTO users ( name ) values ( ? )"
try: try:
with self.DBCON as con: with sqlite3.connect(self.DATABASE, detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES) as con:
con.execute(sql, [username]) con.execute(sql, [username])
except sqlite3.Error as err: except sqlite3.Error as err:
logging.error('User could not be saved in database') logging.error('User could not be saved in database')
@@ -316,7 +315,7 @@ class TimeTrack:
sql = "SELECT * FROM users" sql = "SELECT * FROM users"
try: try:
with self.DBCON as con: with sqlite3.connect(self.DATABASE, detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES) as con:
cur = con.cursor() cur = con.cursor()
if data: if data:
cur.execute(sql, [data]) cur.execute(sql, [data])
@@ -357,7 +356,7 @@ class TimeTrack:
sql = "INSERT INTO active_events ( starttime, user_id ) VALUES ( ?, ? )" sql = "INSERT INTO active_events ( starttime, user_id ) VALUES ( ?, ? )"
try: try:
with self.DBCON as con: with sqlite3.connect(self.DATABASE, detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES) as con:
con.execute(sql, [TIME, self.USERID]) con.execute(sql, [TIME, self.USERID])
except sqlite3.Error as err: except sqlite3.Error as err:
logging.error('Event could not be created') logging.error('Event could not be created')
@@ -390,7 +389,7 @@ class TimeTrack:
return 0 return 0
try: try:
with self.DBCON as con: with sqlite3.connect(self.DATABASE, detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES) as con:
con.execute(sql, [data]) con.execute(sql, [data])
except sqlite3.Error as err: except sqlite3.Error as err:
logging.error('Event could not be deleted') logging.error('Event could not be deleted')
@@ -493,7 +492,7 @@ class TimeTrack:
sql = "INSERT INTO time_entries ( starttime, endtime, user_id, activity ) VALUES ( ?, ?, ?, ? )" sql = "INSERT INTO time_entries ( starttime, endtime, user_id, activity ) VALUES ( ?, ?, ?, ? )"
try: try:
with self.DBCON as con: with sqlite3.connect(self.DATABASE, detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES) as con:
con.execute(sql, [data[1], endtime, self.USERID, action]) con.execute(sql, [data[1], endtime, self.USERID, action])
except sqlite3.Error as err: except sqlite3.Error as err:
logging.error('Time entry could not be created') logging.error('Time entry could not be created')