-
Notifications
You must be signed in to change notification settings - Fork 7
/
reciter_create_table.py
63 lines (51 loc) · 1.97 KB
/
reciter_create_table.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
57
58
59
60
61
62
63
import time
import os
import pymysql.cursors
import pymysql.err
import datetime, time
def run_sql_file(filename, connection):
'''
The function takes a filename and a connection as input
and will run the SQL query on the given connection
'''
MYSQL_OPTION_MULTI_STATEMENTS_ON = 0
start = time.time()
print("Start executing: " + filename + " at " + str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M")))
cursor = connection.cursor()
with open(filename, encoding="utf-8") as f:
commands = f.read().split(';')
for command in commands:
cursor.execute(command)
print(command)
connection.commit()
end = time.time()
print("Time elapsed to run the query:")
print(str((end - start)*1000) + ' ms')
def connect_mysql_server(username, db_password, db_hostname, database_name):
"""Establish a connection to MySQL or MariaDB server. This function is
depandend on the PyMySQL library.
See: https://github.com/PyMySQL/PyMySQL
Args:
username (string): username of the database user.
password (string): password of the database user.
db_hostname (string): hostname or IP address of the database server.
database_name (string): the name of the database we are connecting to.
Returns:
MySQLConnection object.
"""
return pymysql.connect(user=username,
password=db_password,
host=db_hostname,
database=database_name)
def main():
DB_USERNAME = os.getenv('DB_USERNAME')
DB_PASSWORD = os.getenv('DB_PASSWORD')
DB_HOST = os.getenv('DB_HOST')
DB_NAME = os.getenv('DB_NAME')
databaseConnection = connect_mysql_server(DB_USERNAME, DB_PASSWORD, DB_HOST, DB_NAME)
#run_sql_file("reciterAnalysis.sql", databaseConnection)
print(DB_NAME)
print(DB_HOST)
databaseConnection.close()
if __name__ == "__main__":
main()