From 3db70a8486b68350c6e03e73b459181a4b5c9814 Mon Sep 17 00:00:00 2001
From: anima
Date: Sun, 30 May 2021 23:26:18 +0200
Subject: [PATCH] remove db_connect funtion and fix all db connections
---
timeTrack.py | 172 ++++++++++++++++++++-------------------------------
1 file changed, 67 insertions(+), 105 deletions(-)
diff --git a/timeTrack.py b/timeTrack.py
index e57a820..80bfb38 100644
--- a/timeTrack.py
+++ b/timeTrack.py
@@ -58,6 +58,11 @@ class TimeTrack:
logging.info('config file successfully created')
def db_setup(self):
+ if os.path.isfile(self.DATABASE):
+ logging.info('DB was found')
+ else:
+ logging.info('DB not found, create new')
+
sql = []
sql.append("""
CREATE TABLE IF NOT EXISTS time_entries (
@@ -102,132 +107,89 @@ class TimeTrack:
name TEXT NOT NULL
)
""")
- self.db_connect(sql)
+
+ connect = sqlite3.connect(self.DATABASE)
+ cursor = connect.cursor()
+ 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
+ connect.close()
+
##> Kürzer möglich ?
def db_connect(self, SQL, DATA=''):
- if os.path.isfile(self.DATABASE):
- logging.info('DB was found')
+ pass
+
+ def create_user(self, USER=''):
+ if USER == '':
+ username = input('Enter your Name: ')
else:
- logging.info('DB not found, create new')
+ username = USER
+
+ while self.get_users(NAME=username):
+ print('Name ist schon vergeben')
+ username = input('Wähle einen anderen Namen: ')
connect = sqlite3.connect(self.DATABASE)
cursor = connect.cursor()
- 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
-
+ sql = "INSERT INTO users ( name ) values ( ? )"
+
+ try:
+ cursor.execute(sql, [username])
+ except:
+ logging.error('Fail to execute SQL command')
+ logging.debug(sql)
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()
+ logging.info('Success execute SQL command')
+ connect.commit()
+ connect.close()
- 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)
- logging.debug(data)
- else:
- logging.info('Success execute SQL command')
- logging.debug(sql)
- connect.commit()
-
- output = []
- for data in cursor:
- output.append(data)
- connect.close()
- return output
-
- def create_user(self, USER=''):
- if USER == '':
- data = input('Enter your Name: ')
- else:
- data = USER
-
-
- while self.get_users(NAME=data):
- print('Name ist schon vergeben')
- data = input('Wähle einen anderen Namen: ')
-
- sql = """
- INSERT INTO users (
- name
- ) values (
- ?
- )
- """
- self.db_connect(sql, data)
-
##> verbugt noch nicht funktional
def get_users(self, UID=0, NAME=''):
if not UID == 0:
logging.info('UID Used')
data = UID
- sql = """
- SELECT name FROM users WHERE id = ?
- """
+ sql = "SELECT name FROM users WHERE id = ?"
elif not NAME == '':
logging.info('NAME used')
data = NAME
- sql = """
- SELECT name FROM users WHERE name = ?
- """
+ sql = "SELECT name FROM users WHERE name = ?"
else:
logging.info('nothing used')
- sql = """
- SELECT * FROM users WHERE
- """
+ data = ''
+ sql = "SELECT * FROM users"
- return self.db_connect(sql, data)
+ connect = sqlite3.connect(self.DATABASE)
+ cursor = connect.cursor()
+
+ try:
+ if data:
+ cursor.execute(sql, [data])
+ else:
+ cursor.execute(sql)
+
+ except:
+ logging.error('Fail to execute SQL command')
+ logging.debug(sql)
+ else:
+ logging.info('Success execute SQL command')
+ connect.commit()
+
+ row = cursor.fetchall()
+ output = []
+ for data in row:
+ print(data)
+ output.append(data)
+ connect.close()
+ return output
def set_user(self):
pass