-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.py
executable file
·107 lines (83 loc) · 3.24 KB
/
Main.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# import all required modules
import psycopg2
from CarColor import create_car_color
from CarCategory import create_car_category
from CarEngine import create_car_engine
from CarModel import create_car_model
from CarVariant import create_car_variant
from Car import create_car
from Customer import create_customer
from SalesPerson import create_salesperson
from Installment import create_installment
from Payment import create_payment
from Sale import create_sale
from Finance import create_finance
# -----------------------------------------------Connection and Database Creation -----------------------------------------------------
def create_connection_and_newdb(database_name):
# Connect to the PostgreSQL server (postgres DB) with autocommit mode
# try except for connection
try:
connection = psycopg2.connect(
host="localhost",
user="postgres",
password="root",
dbname="postgres",
port=5432)
connection.autocommit = True # Disable transactions
except psycopg2.Error as e:
print("Error: Unable to connect to the database")
print(e)
return None
# -------------------------------------------------------- DROP & CREATE -------------------------------------------------------------
# try except to drop and create database
try:
with connection.cursor() as cursor:
# Check if the database already exists
cursor.execute(f"SELECT 1 FROM pg_database WHERE datname = '{database_name}'")
exists = cursor.fetchone()
if exists:
# Drop the existing database
cursor.execute(f"DROP DATABASE {database_name}")
print(f"Database '{database_name}' dropped successfully.")
# Create a new database
cursor.execute(f"CREATE DATABASE {database_name}")
print(f"Database '{database_name}' created successfully.")
except psycopg2.Error as e:
print(f"Error: Unable to create or drop the database '{database_name}'")
print(e)
return None
# ---------------------------- Create tables and Poulate data ----------------------------------------------------------------
# Function to create tables and populate data
def create_tables_and_populate_data(database_name):
# Establish connection to the new created database
db = psycopg2.connect(
host="localhost",
user="postgres",
password="root",
dbname=database_name,
port=5432)
if db is None:
return
cursor = db.cursor()
# Calling other scripts to populate tables
create_car_color(db)
create_car_category(db)
create_car_engine(db)
create_car_model(db)
create_car_variant(db)
create_car(db)
create_customer(db)
create_salesperson(db)
create_installment(db)
create_payment(db)
create_sale(db)
create_finance(db)
# Commit changes and close the connection
db.commit()
cursor.close()
db.close()
# Driver Code
if __name__ == "__main__":
database_name = input("Enter the database name: ")
create_connection_and_newdb(database_name)
create_tables_and_populate_data(database_name)