-
Notifications
You must be signed in to change notification settings - Fork 2
/
create.py
56 lines (40 loc) · 1.85 KB
/
create.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import copy
from pathlib import Path
from database import (database_exists,
create_database,
table_exists,
create_database_connection,
create_database_cursor)
from tables import (config_from_xml_file,
create_mysql_table,
load_data_into_mysql_table)
CONFIG_FILE = str(Path(__file__).parent / 'config.xml')
def main():
config = config_from_xml_file(CONFIG_FILE)
mysql_config = copy.deepcopy(config['mysql_config'])
del mysql_config['database'] # databases can't be checked or created if a database is specified
database_connection = create_database_connection(mysql_config)
database_cursor = create_database_cursor(database_connection)
if database_exists(config, database_cursor):
print("'{}' database already exists.".format(config['database_name']))
else:
create_database(config, database_cursor)
print()
database_cursor.stop_cursor()
database_connection.disconnect()
database_connection = create_database_connection(config['mysql_config'])
database_cursor = create_database_cursor(database_connection)
for table in config['tables'].keys():
if table_exists(table, database_cursor):
print("'{}' table already exists.".format(table))
else:
create_mysql_table(config, table, database_connection, database_cursor)
print("'{}' table has been created.".format(table))
print("Populating '{}' table with data......".format(table))
load_data_into_mysql_table(config, table, database_connection, database_cursor)
print("Population of '{}' table has been completed".format(table))
print()
database_cursor.stop_cursor()
database_connection.disconnect()
if __name__ == '__main__':
main()