|
1 | 1 | import mysql.connector as sql |
2 | | -con=sql.connect(host='localhost', user=input("Enter your account name:"), passwd=input('Enter your password:'), auth_plugin='mysql_native_password') |
3 | | -cursor_var=con.cursor() |
4 | | - |
5 | | -def database_creation(): |
6 | | - """ |
7 | | - Creating database using and tables (CHECK_IN and CHECK_OUT). |
8 | | - """ |
9 | | - create='CREATE DATABASE ROOM_MANAGEMENT_SYSTEM' |
10 | | - cursor_var.execute(create) |
11 | | - print("DATABASE CREATED SUCCESSFULLY!!!") |
12 | | - |
13 | | - use='USE ROOM_MANAGEMENT_SYSTEM' |
14 | | - cursor_var.execute(use) |
15 | | - create_checkin='CREATE TABLE CHECK_IN(ROOM_TYPE VARCHAR(20), NO_OF_DAYS INT, NAME VARCHAR(20), AADHAAR_CARD_NO VARCHAR(12), MOBILE_NO VARCHAR(13), DATE VARCHAR(20), ROOM_NO INT)' |
16 | | - cursor_var.execute(create_checkin) |
17 | | - print("CHECK_IN TABLE CREATED SUCCESSFULLY!!!") |
18 | | - |
19 | | - create_checkout='CREATE TABLE CHECK_OUT(ROOM_TYPE VARCHAR(30), ROOM_NO INT, NAME VARCHAR(20), MOBILE_NO VARCHAR(13), AADHAAR_CARD_NO VARCHAR(12), NO_OF_DAYS INT, TOTAL_BILL FLOAT, DATE_OF_BILLING VARCHAR(20))' |
20 | | - cursor_var.execute(create_checkout) |
21 | | - print("CHECK_OUT TABLE CREATED SUCCESSFULLY!!!") |
22 | | - |
23 | | - con.commit() |
24 | | - main() |
25 | | - |
26 | | -def database_deletion(): |
27 | | - |
28 | | - """ |
29 | | - Deleting database. |
30 | | - """ |
31 | | - print(" ") |
32 | | - print("Press 1 for to delete entire database.") |
33 | | - print("Press 2 to delete tables in database.") |
34 | | - print(" ") |
35 | | - |
36 | | - user_choice=int(input("Enter your choice:")) |
37 | | - if user_choice==1: |
38 | | - drop_database='DROP DATABASE ROOM_MANAGEMENT_SYSTEM' |
39 | | - cursor_var.execute(drop_database) |
40 | | - print("DATABASE deleted successfully!!!") |
41 | | - |
| 2 | +from mysql.connector import Error |
| 3 | + |
| 4 | +try: |
| 5 | + username = input("Enter your username:") |
| 6 | + password = input("Enter your password:") |
| 7 | + con = sql.connect(host = "localhost", user = username, passwd = password, auth_plugin = "mysql_native_password") |
| 8 | + |
| 9 | + if con.is_connected(): |
| 10 | + print("Connection successful!") |
| 11 | + cursor_var = con.cursor() |
| 12 | +except Error as e: |
| 13 | + print(f'Connection failed to MySQL, error:{e}') |
| 14 | + |
| 15 | + |
| 16 | +class DataBaseCreation: |
| 17 | + '''Creating the database, Room Management System''' |
| 18 | + def __init__(self, connection): |
| 19 | + self.connection = connection |
| 20 | + self.cursor = self.connection.cursor() |
| 21 | + |
| 22 | + def database_create(self): |
| 23 | + try: |
| 24 | + create = "CREATE DATABASE IF NOT EXISTS ROOM_MANAGEMENT_SYSTEM" |
| 25 | + self.cursor.execute(create) |
| 26 | + self.connection.commit() |
| 27 | + print("Database created or already exists.") |
| 28 | + except Error as e: |
| 29 | + print(f"Unable to create Database, error: {e}") |
| 30 | + |
| 31 | + def UseDatabase(self): |
| 32 | + try: |
| 33 | + database = "USE ROOM_MANAGEMENT_SYSTEM" |
| 34 | + self.cursor.execute(database) |
| 35 | + self.connection.commit() |
| 36 | + except Error as e: |
| 37 | + print(f"Error: {e}") |
| 38 | + |
| 39 | + def CreateTable(self): |
| 40 | + try: |
| 41 | + create_checkin = ''' |
| 42 | + CREATE TABLE IF NOT EXISTS CHECK_IN ( |
| 43 | + ROOM_TYPE VARCHAR(20), |
| 44 | + NO_OF_DAYS INT, |
| 45 | + NAME VARCHAR(20), |
| 46 | + AADHAAR_CARD_NO VARCHAR(12), |
| 47 | + MOBILE_NO VARCHAR(13), |
| 48 | + DATE VARCHAR(20), |
| 49 | + ROOM_NO INT |
| 50 | + ) |
| 51 | + ''' |
| 52 | + self.cursor.execute(create_checkin) |
| 53 | + |
| 54 | + create_checkout = ''' |
| 55 | + CREATE TABLE IF NOT EXISTS CHECK_OUT ( |
| 56 | + ROOM_TYPE VARCHAR(30), |
| 57 | + ROOM_NO INT, |
| 58 | + NAME VARCHAR(20), |
| 59 | + MOBILE_NO VARCHAR(13), |
| 60 | + AADHAAR_CARD_NO VARCHAR(12), |
| 61 | + NO_OF_DAYS INT, |
| 62 | + TOTAL_BILL FLOAT, |
| 63 | + DATE_OF_BILLING VARCHAR(20) |
| 64 | + ) |
| 65 | + ''' |
| 66 | + self.cursor.execute(create_checkout) |
| 67 | + |
| 68 | + self.connection.commit() |
| 69 | + print("Both tables created (if not already existing).") |
| 70 | + except Error as e: |
| 71 | + print(f"Error: {e}") |
| 72 | + |
| 73 | + |
| 74 | +class DataBaseDeletion: |
| 75 | + '''Deleting database, Room Management System''' |
| 76 | + def __init__(self, connection): |
| 77 | + self.connection = connection |
| 78 | + self.cursor = self.connection.cursor() |
| 79 | + |
| 80 | + def perform_deletion(self): |
| 81 | + print("\nPress 1 to delete entire database.") |
| 82 | + print("Press 2 to delete tables in database.\n") |
| 83 | + user_choice = int(input("Enter your choice: ")) |
| 84 | + |
| 85 | + if user_choice == 1: |
| 86 | + try: |
| 87 | + self.cursor.execute("DROP DATABASE IF EXISTS ROOM_MANAGEMENT_SYSTEM") |
| 88 | + print("DATABASE deleted successfully!") |
| 89 | + except Error as e: |
| 90 | + print(f"Error: {e}") |
42 | 91 | elif user_choice == 2: |
43 | | - print(" ") |
44 | | - print("Press 1 to delete both tables.") |
| 92 | + self.cursor.execute("USE ROOM_MANAGEMENT_SYSTEM") |
| 93 | + print("\nPress 1 to delete both tables.") |
45 | 94 | print("Press 2 to delete CHECK_IN table only.") |
46 | | - print("Press 3 to delete CHECK_OUT table only.") |
47 | | - print(" ") |
48 | | - user_choice_table = int(input("Enter your choice:")) |
49 | | - |
50 | | - if user_choice_table == 1: |
51 | | - cursor_var.execute("DROP TABLE IF EXISTS CHECK_IN") |
52 | | - cursor_var.execute("DROP TABLE IF EXISTS CHECK_OUT") |
53 | | - print("Both tables deleted successfully") |
54 | | - elif user_choice_table == 2: |
55 | | - cursor_var.execute("DROP TABLE IF EXISTS CHECK_IN") |
56 | | - print("CHECK_IN table deleted successfully") |
57 | | - elif user_choice_table == 3: |
58 | | - cursor_var.execute("DROP TABLE IF EXISTS CHECK_OUT") |
59 | | - print("CHECK_OUT table deleted successfully") |
60 | | - |
61 | | - con.commit() |
62 | | - main() |
| 95 | + print("Press 3 to delete CHECK_OUT table only.\n") |
| 96 | + user_choice_table = int(input("Enter your choice: ")) |
| 97 | + try: |
| 98 | + if user_choice_table == 1: |
| 99 | + self.cursor.execute("DROP TABLE IF EXISTS CHECK_IN") |
| 100 | + self.cursor.execute("DROP TABLE IF EXISTS CHECK_OUT") |
| 101 | + print("Both tables deleted successfully.") |
| 102 | + elif user_choice_table == 2: |
| 103 | + self.cursor.execute("DROP TABLE IF EXISTS CHECK_IN") |
| 104 | + print("CHECK_IN table deleted successfully.") |
| 105 | + elif user_choice_table == 3: |
| 106 | + self.cursor.execute("DROP TABLE IF EXISTS CHECK_OUT") |
| 107 | + print("CHECK_OUT table deleted successfully.") |
| 108 | + else: |
| 109 | + print("Invalid choice.") |
| 110 | + self.connection.commit() |
| 111 | + except Error as e: |
| 112 | + print(f"Error: {e}") |
| 113 | + |
| 114 | + |
| 115 | +class ColumnModification: |
| 116 | + '''Help to modify the columns''' |
| 117 | + def __init__(self, connection): |
| 118 | + self.connection = connection |
| 119 | + self.cursor = self.connection.cursor() |
| 120 | + |
| 121 | + def modify_column(self): |
| 122 | + self.cursor.execute("USE ROOM_MANAGEMENT_SYSTEM") |
| 123 | + print("\nPress 1 to ADD column.") |
| 124 | + print("Press 2 to DELETE column.\n") |
| 125 | + try: |
| 126 | + choice = int(input("Enter your choice: ")) |
| 127 | + table = input("Enter table name (CHECK_IN/CHECK_OUT): ").upper() |
| 128 | + column_name = input("Enter column name: ") |
| 129 | + |
| 130 | + if choice == 1: |
| 131 | + column_type = input("Enter column type (e.g. VARCHAR(20), INT): ") |
| 132 | + query = f"ALTER TABLE {table} ADD COLUMN {column_name} {column_type}" |
| 133 | + self.cursor.execute(query) |
| 134 | + print("Column added successfully.") |
| 135 | + elif choice == 2: |
| 136 | + query = f"ALTER TABLE {table} DROP COLUMN {column_name}" |
| 137 | + self.cursor.execute(query) |
| 138 | + print("Column deleted successfully.") |
| 139 | + else: |
| 140 | + print("Invalid option.") |
| 141 | + self.connection.commit() |
| 142 | + except Error as e: |
| 143 | + print(f"Error: {e}") |
| 144 | + except Exception as ex: |
| 145 | + print(f"Invalid input: {ex}") |
| 146 | + |
| 147 | + |
| 148 | +class IndividualTableCreation: |
| 149 | + '''Can create individual tables also''' |
| 150 | + def __init__(self, connection): |
| 151 | + self.connection = connection |
| 152 | + self.cursor = self.connection.cursor() |
| 153 | + |
| 154 | + def create_checkin_table(self): |
| 155 | + try: |
| 156 | + self.cursor.execute(''' |
| 157 | + CREATE TABLE IF NOT EXISTS CHECK_IN ( |
| 158 | + ROOM_TYPE VARCHAR(20), |
| 159 | + NO_OF_DAYS INT, |
| 160 | + NAME VARCHAR(20), |
| 161 | + AADHAAR_CARD_NO VARCHAR(12), |
| 162 | + MOBILE_NO VARCHAR(13), |
| 163 | + DATE VARCHAR(20), |
| 164 | + ROOM_NO INT |
| 165 | + ) |
| 166 | + ''') |
| 167 | + self.connection.commit() |
| 168 | + print("CHECK_IN table created successfully.") |
| 169 | + except Error as e: |
| 170 | + print(f"Error creating CHECK_IN table: {e}") |
| 171 | + |
| 172 | + def create_checkout_table(self): |
| 173 | + try: |
| 174 | + self.cursor.execute(''' |
| 175 | + CREATE TABLE IF NOT EXISTS CHECK_OUT ( |
| 176 | + ROOM_TYPE VARCHAR(30), |
| 177 | + ROOM_NO INT, |
| 178 | + NAME VARCHAR(20), |
| 179 | + MOBILE_NO VARCHAR(13), |
| 180 | + AADHAAR_CARD_NO VARCHAR(12), |
| 181 | + NO_OF_DAYS INT, |
| 182 | + TOTAL_BILL FLOAT, |
| 183 | + DATE_OF_BILLING VARCHAR(20) |
| 184 | + ) |
| 185 | + ''') |
| 186 | + self.connection.commit() |
| 187 | + print("CHECK_OUT table created successfully.") |
| 188 | + except Error as e: |
| 189 | + print(f"Error creating CHECK_OUT table: {e}") |
| 190 | + |
| 191 | + def perform_table_creation(self): |
| 192 | + self.cursor.execute("USE ROOM_MANAGEMENT_SYSTEM") |
| 193 | + print("\nPress 1 to create CHECK_IN table.") |
| 194 | + print("Press 2 to create CHECK_OUT table.") |
| 195 | + print("Press 3 to create both tables.\n") |
| 196 | + try: |
| 197 | + user_choice = int(input("Enter your choice: ")) |
| 198 | + if user_choice == 1: |
| 199 | + self.create_checkin_table() |
| 200 | + elif user_choice == 2: |
| 201 | + self.create_checkout_table() |
| 202 | + elif user_choice == 3: |
| 203 | + self.create_checkin_table() |
| 204 | + self.create_checkout_table() |
| 205 | + else: |
| 206 | + print("Invalid choice.") |
| 207 | + except Exception as e: |
| 208 | + print(f"Error: {e}") |
| 209 | + |
63 | 210 |
|
64 | 211 | def main(): |
65 | | - print("Enter 1 to create database 'ROOM_MANAGEMENT_SYSTEM'") |
66 | | - print("Enter 2 to drop database/table from 'ROOM_MANAGEMENT_SYSTEM'") |
67 | | - user_choice=int(input("Enter your choice:")) |
68 | | - if user_choice==1: |
69 | | - database_creation() |
70 | | - elif user_choice==2: |
71 | | - database_deletion() |
| 212 | + print("\n==== ROOM MANAGEMENT SYSTEM ====") |
| 213 | + print("1. Create database and both tables") |
| 214 | + print("2. Drop database or specific tables") |
| 215 | + print("3. Add/Delete column from table") |
| 216 | + print("4. Create individual tables (if not exist)") |
| 217 | + print("0. Exit") |
| 218 | + |
| 219 | + try: |
| 220 | + user_choice = int(input("Enter your choice: ")) |
| 221 | + if user_choice == 1: |
| 222 | + db = DataBaseCreation(con) |
| 223 | + db.database_create() |
| 224 | + db.UseDatabase() |
| 225 | + db.CreateTable() |
| 226 | + elif user_choice == 2: |
| 227 | + deleter = DataBaseDeletion(con) |
| 228 | + deleter.perform_deletion() |
| 229 | + elif user_choice == 3: |
| 230 | + modifier = ColumnModification(con) |
| 231 | + modifier.modify_column() |
| 232 | + elif user_choice == 4: |
| 233 | + creator = IndividualTableCreation(con) |
| 234 | + creator.perform_table_creation() |
| 235 | + elif user_choice == 0: |
| 236 | + print("Exiting program.") |
| 237 | + return |
72 | 238 | else: |
73 | | - print("Wrong input...") |
74 | | - main() |
75 | | -main() |
| 239 | + print("Invalid input.") |
| 240 | + except Exception as e: |
| 241 | + print(f"Error in main menu: {e}") |
| 242 | + |
| 243 | + main() # Recursive loop |
| 244 | + |
| 245 | +if __name__ == "__main__": |
| 246 | + main() |
0 commit comments