From efff88d5fd731f4bada5b477c4bf72486d294e4e Mon Sep 17 00:00:00 2001 From: anima Date: Thu, 13 May 2021 18:22:23 +0200 Subject: [PATCH] db handling optimized --- timeTrack.py | 130 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 88 insertions(+), 42 deletions(-) diff --git a/timeTrack.py b/timeTrack.py index bda91e0..73c2f13 100644 --- a/timeTrack.py +++ b/timeTrack.py @@ -104,63 +104,108 @@ class TimeTrack: """) self.db_connect(sql) + ##> Kürzer möglich ? def db_connect(self, SQL, DATA=''): if os.path.isfile(self.DATABASE): - logging.info('DB gefunden') + logging.info('DB was found') else: - logging.info('Keine DB gefunden, neue wird angelegt') + logging.info('DB not found, create new') connect = sqlite3.connect(self.DATABASE) cursor = connect.cursor() - ##> vorgang optimieren... - ##> prüfung if DATA list - ##> prüfung if init SQL list - for sql in SQL: - try: - if isinstance(sql, list): - logging.debug('SQL is list') - if DATA == '': - logging.info('Many execute without data') - cursor.executemany(sql) - else: - logging.info('Many execute with data') - cursor.executemany(sql, DATA) - else: - logging.debug('SQL is not list ') - if DATA == '': - logging.info('single execute without data') - cursor.execute(sql) - else: - logging.info('single execute with data') - cursor.execute(sql, DATA) - except: - logging.error('Fehler beim schreiben in DB') - logging.debug(sql) - else: - logging.info('Erfolgreich in DB geschrieben') - logging.debug(sql) - connect.commit() - pass - connect.close() - def create_user(self): - data = [] - sql = [] - data.append(input('Enter your Name: ')) - sql.append(""" + if isinstance(SQL, list): + logging.info('Multible SQL commands found') + for sql in SQL: + logging.debug('Run SQL') + try: + cursor.execute(sql) + except: + logging.error('Fail to execute SQL command') + logging.debug(sql) + else: + logging.info('Success execute SQL command') + connect.commit + + else: + logging.info('Single SQL command found') + sql = SQL + if DATA == '': + logging.debug('Run SQL') + try: + cursor.execute(sql) + except: + logging.error('Fail to execute SQL command') + logging.debug(sql) + else: + logging.info('Success execute SQL command') + connect.commit() + + else: + logging.info('Data found') + data = DATA + if isinstance(DATA, list): + logging.debug('Data is list') + if isinstance(DATA[0], list): + try: + cursor.executemany(sql, data) + except: + logging.error('Fail to execute SQL command') + logging.debug(sql) + else: + logging.info('Success execute SQL command') + logging.debug(sql) + connect.commit() + else: + try: + cursor.execute(sql, data) + except: + logging.error('Fail to execute SQL command') + logging.debug(sql) + else: + logging.info('Success execute SQL command') + logging.debug(sql) + connect.commit() + + else: + logging.debug('Convert Data to list') + logging.debug('Run SQL') + try: + cursor.execute(sql, [data]) + except: + logging.error('Fail to execute SQL command') + logging.debug(sql) + else: + logging.info('Success execute SQL command') + logging.debug(sql) + connect.commit() + + output = [] + for data in cursor: + output.append(data) + connect.close() + print(output) + return output + + def create_user(self, USER=''): + if USER == '': + data = input('Enter your Name: ') + else: + data = USER + + sql = """ INSERT INTO users ( name ) values ( ? ) - """) + """ self.db_connect(sql, data) pass ##> verbugt noch nicht funktional - def get_users(self, UID=0): - data = [] - data.append(input('User ID: ')) + def get_users(self, UID=0, NAME=''): + data = input('User ID: ') if UID == 0: sql = [ """ @@ -173,11 +218,12 @@ class TimeTrack: SELET id, name FROM users WHERE id=? """ ] - self.db_connect(sql) + print(self.db_connect(sql)) def set_user(self): pass test = TimeTrack() +#test.create_user() test.get_users() \ No newline at end of file