From e0bc13d092df2e9e4853137bd88660a3cd0f716d Mon Sep 17 00:00:00 2001 From: Thomas Kuschel Date: Sun, 23 Jun 2024 20:47:05 +0200 Subject: [PATCH] ADD create database (when enough privileges) --- afu/callbook.py | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/afu/callbook.py b/afu/callbook.py index 99c4cdb..fab00f4 100755 --- a/afu/callbook.py +++ b/afu/callbook.py @@ -452,6 +452,32 @@ def exec_sql_file(cursor, sql_file): except Exception as e: print('Error: {}'.format(e), file=sys.stderr) +def call_create_database(configdb): + database = configdb.get('database','callbook') + print(f'Try to create the database "{database}" (if you have enough privileges)') + conn = mariadb.connect( + host = configdb.get('host','127.0.0.1'), + user = configdb.get('user','om'), + password = configdb.get('password','oe3tkt') + ) + # create a cursor object + cur = conn.cursor() + # creating database + try: + cur.execute('CREATE DATABASE ' + database) + except mariadb.Error as e: + print(f'\n[ERROR] MySQLError during execute statement\n\tArgs: {e.args}') + sys.exit(5) + cur.execute('SHOW DATABASES') + databaseList = cur.fetchall() + #for database in databaseList: + # print(database) + if not databaseList: + conn.close() + sys.exit(5) + cur.execute('USE ' + database) + return conn + if __name__ == '__main__': args = call_parser() config = call_configparser() @@ -475,7 +501,8 @@ if __name__ == '__main__': ) except mariadb.Error as e: print(f'Error connectiong to MariaDB platform: {e}') - sys.exit(5) + conn = call_create_database(configdb) + print(f'Connected to MariaDB API Version {mariadb.mariadbapi_version}.') print(datetime.datetime.now(datetime.UTC)) @@ -483,7 +510,6 @@ if __name__ == '__main__': cur = conn.cursor() cur.execute('SELECT VERSION()') print(cur.fetchone()[0]) - sql_file = '.sql_init' path = os.path.join(os.path.dirname(os.path.abspath(__file__)), sql_file) exec_sql_file(cur, path)