-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_db.py
73 lines (65 loc) · 2.82 KB
/
create_db.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
64
65
66
67
68
69
70
71
72
73
import os
import pyodbc
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# # Get individual components from environment variables
# driver = os.getenv("DB_DRIVER")
# server = os.getenv("DB_SERVER")
# database = 'master' # Connect to the master database to create a new database
# username = os.getenv("DB_USERNAME")
# password = os.getenv("DB_PASSWORD")
# # Construct the connection string
# connection_string = f'DRIVER={{{driver}}};SERVER={{{server}}};DATABASE={database};UID={username};PWD={password}'
# DB_DATABASE = os.getenv("DB_DATABASE")
# # SQL command to create the database (change the db name if required)
# create_db_query = f"IF NOT EXISTS (SELECT * FROM sys.databases WHERE name = '{DB_DATABASE}') CREATE DATABASE FYP;"
# try:
# # Connect to the SQL Server
# with pyodbc.connect(
# driver=f'{{{driver}}}',
# server=server,
# database=database,
# uid=username,
# pwd=password,
# autocommit=True,
# ) as conn:
# with conn.cursor() as cursor:
# cursor.execute(create_db_query)
# print(f"Database '{DB_DATABASE}' created or already exists.")
# except Exception as e:
# print(f"An error occurred: {e}")
# Get individual components from environment variables
driver = os.getenv("DB_DRIVER")
#ntu
#server = os.getenv("DB_SERVER")
#local
server = os.getenv("DB_SERVER_DEV")
DB_DATABASE = os.getenv("DB_DATABASE")
username = os.getenv("DB_USERNAME")
password = os.getenv("DB_PASSWORD")
# Construct the connection string
#connection_string = f'DRIVER={{{driver}}};SERVER={{{server}}};DATABASE={database};UID={username};PWD={password}'
# SQL command to create the database (change the db name if required)
create_db_query = f"IF NOT EXISTS (SELECT * FROM sys.databases WHERE name = '{DB_DATABASE}') CREATE DATABASE [{DB_DATABASE}];"
# SQL command to create the database (change the db name if required)
create_db_query = "IF NOT EXISTS (SELECT * FROM sys.databases WHERE name = 'user_service_dev') CREATE DATABASE user_service_dev;"
connection_string = (
f"DRIVER={{{driver}}};"
f"SERVER={server};"
f"DATABASE={DB_DATABASE};"
f"Trusted_Connection=yes;"
)
try:
#ntu
#connection_string = f"DRIVER={driver};SERVER={server};DATABASE={DB_DATABASE};UID={username};PWD={password};"
#local
connection_string = (f"DRIVER={{{driver}}};"f"SERVER={server};"f"DATABASE={DB_DATABASE};"f"Trusted_Connection=yes;")
with pyodbc.connect(connection_string, autocommit=True) as conn:
with conn.cursor() as cursor:
cursor.execute(create_db_query)
print(f"Database {DB_DATABASE} created or already exists.")
except Exception as e:
print("Error details:")
print(f"Driver: {driver}, Server: {server}, Database: {DB_DATABASE}, User: {username}")
print(f"An error occurred: {e}")