-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_database.py
43 lines (31 loc) · 1.04 KB
/
setup_database.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
import mysql.connector
import os
from dotenv import load_dotenv
load_dotenv()
DB_USERNAME = os.getenv("DB_USERNAME")
DB_PASSWORD = os.getenv("DB_PASSWORD")
DB_NAME = os.getenv("DB_NAME")
DB_HOST = os.getenv("DB_HOST")
SECRET_KEY = os.getenv("SECRET_KEY")
def run_schema_sql(file):
try:
connection = mysql.connector.connect(
user=DB_USERNAME, password=DB_PASSWORD, host=DB_HOST, database=DB_NAME
)
cursor = connection.cursor()
with open(file, "r") as sql_file:
sql_commands = sql_file.read().split(";")
for sql_command in sql_commands:
if sql_command.strip():
cursor.execute(sql_command)
connection.commit()
print("Database Initialized. File Executed Successfully!")
except mysql.connector.Error as error:
print(f"Error: {error}")
finally:
if connection:
cursor.close()
connection.close()
if __name__ == "__main__":
file = "schema.sql"
run_schema_sql(file)