ADD create database (when enough privileges)

This commit is contained in:
Thomas Kuschel 2024-06-23 20:47:05 +02:00
parent 3a8032b787
commit e0bc13d092

View File

@ -452,6 +452,32 @@ def exec_sql_file(cursor, sql_file):
except Exception as e: except Exception as e:
print('Error: {}'.format(e), file=sys.stderr) 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__': if __name__ == '__main__':
args = call_parser() args = call_parser()
config = call_configparser() config = call_configparser()
@ -475,7 +501,8 @@ if __name__ == '__main__':
) )
except mariadb.Error as e: except mariadb.Error as e:
print(f'Error connectiong to MariaDB platform: {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(f'Connected to MariaDB API Version {mariadb.mariadbapi_version}.')
print(datetime.datetime.now(datetime.UTC)) print(datetime.datetime.now(datetime.UTC))
@ -483,7 +510,6 @@ if __name__ == '__main__':
cur = conn.cursor() cur = conn.cursor()
cur.execute('SELECT VERSION()') cur.execute('SELECT VERSION()')
print(cur.fetchone()[0]) print(cur.fetchone()[0])
sql_file = '.sql_init' sql_file = '.sql_init'
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), sql_file) path = os.path.join(os.path.dirname(os.path.abspath(__file__)), sql_file)
exec_sql_file(cur, path) exec_sql_file(cur, path)