|
2 | 2 | import os
|
3 | 3 |
|
4 | 4 |
|
5 |
| -# Making connection with database |
6 |
| -def connect_database(): |
7 |
| - global conn |
8 |
| - global cur |
9 |
| - conn = sqlite3.connect(os.path.join(os.path.dirname(__file__), "bankmanaging.db")) |
10 |
| - cur = conn.cursor() |
11 |
| - cur.execute( |
12 |
| - """ |
13 |
| - CREATE TABLE IF NOT EXISTS bank ( |
14 |
| - acc_no INTEGER PRIMARY KEY, |
15 |
| - name TEXT, |
16 |
| - age INTEGER, |
17 |
| - address TEXT, |
18 |
| - balance INTEGER, |
19 |
| - account_type TEXT, |
20 |
| - mobile_number TEXT |
| 5 | +class DatabaseManager: |
| 6 | + def __init__(self, db_name="bankmanaging.db"): |
| 7 | + self.db_path = os.path.join(os.path.dirname(__file__), db_name) |
| 8 | + self.conn = sqlite3.connect(self.db_path, check_same_thread=False) |
| 9 | + self.cur = self.conn.cursor() |
| 10 | + self._setup_tables() |
| 11 | + self.acc_no = self._get_last_acc_no() + 1 |
| 12 | + |
| 13 | + def _setup_tables(self): |
| 14 | + self.cur.execute(""" |
| 15 | + CREATE TABLE IF NOT EXISTS bank ( |
| 16 | + acc_no INTEGER PRIMARY KEY, |
| 17 | + name TEXT, |
| 18 | + age INTEGER, |
| 19 | + address TEXT, |
| 20 | + balance INTEGER, |
| 21 | + account_type TEXT, |
| 22 | + mobile_number TEXT |
| 23 | + ) |
| 24 | + """) |
| 25 | + self.cur.execute(""" |
| 26 | + CREATE TABLE IF NOT EXISTS staff ( |
| 27 | + name TEXT, |
| 28 | + pass TEXT, |
| 29 | + salary INTEGER, |
| 30 | + position TEXT |
| 31 | + ) |
| 32 | + """) |
| 33 | + self.cur.execute("CREATE TABLE IF NOT EXISTS admin (name TEXT, pass TEXT)") |
| 34 | + self.cur.execute("SELECT COUNT(*) FROM admin") |
| 35 | + if self.cur.fetchone()[0] == 0: |
| 36 | + self.cur.execute("INSERT INTO admin VALUES (?, ?)", ("admin", "admin123")) |
| 37 | + self.conn.commit() |
| 38 | + |
| 39 | + def _get_last_acc_no(self): |
| 40 | + self.cur.execute("SELECT MAX(acc_no) FROM bank") |
| 41 | + last = self.cur.fetchone()[0] |
| 42 | + return last if last else 0 |
| 43 | + |
| 44 | + # ----------------- Admin ----------------- |
| 45 | + def check_admin(self, name, password): |
| 46 | + self.cur.execute("SELECT 1 FROM admin WHERE name=? AND pass=?", (name, password)) |
| 47 | + return self.cur.fetchone() is not None |
| 48 | + |
| 49 | + # ----------------- Staff ----------------- |
| 50 | + def create_employee(self, name, password, salary, position): |
| 51 | + self.cur.execute("INSERT INTO staff VALUES (?, ?, ?, ?)", (name, password, salary, position)) |
| 52 | + self.conn.commit() |
| 53 | + |
| 54 | + def check_employee(self, name, password): |
| 55 | + self.cur.execute("SELECT 1 FROM staff WHERE name=? AND pass=?", (name, password)) |
| 56 | + return self.cur.fetchone() is not None |
| 57 | + |
| 58 | + def show_employees(self): |
| 59 | + self.cur.execute("SELECT name, salary, position FROM staff") |
| 60 | + return self.cur.fetchall() |
| 61 | + |
| 62 | + def update_employee(self, field, new_value, name): |
| 63 | + if field not in {"name", "pass", "salary", "position"}: |
| 64 | + raise ValueError("Invalid employee field") |
| 65 | + self.cur.execute(f"UPDATE staff SET {field}=? WHERE name=?", (new_value, name)) |
| 66 | + self.conn.commit() |
| 67 | + |
| 68 | + def check_name_in_staff(self, name): |
| 69 | + self.cur.execute("SELECT 1 FROM staff WHERE name=?", (name,)) |
| 70 | + return self.cur.fetchone() is not None |
| 71 | + |
| 72 | + # ----------------- Customer ----------------- |
| 73 | + def create_customer(self, name, age, address, balance, acc_type, mobile_number): |
| 74 | + acc_no = self.acc_no |
| 75 | + self.cur.execute( |
| 76 | + "INSERT INTO bank VALUES (?, ?, ?, ?, ?, ?, ?)", |
| 77 | + (acc_no, name, age, address, balance, acc_type, mobile_number) |
21 | 78 | )
|
22 |
| - """ |
23 |
| - ) |
24 |
| - cur.execute( |
25 |
| - """ |
26 |
| - CREATE TABLE IF NOT EXISTS staff ( |
27 |
| - name TEXT, |
28 |
| - pass TEXT, |
29 |
| - salary INTEGER, |
30 |
| - position TEXT |
31 |
| - ) |
32 |
| - """ |
33 |
| - ) |
34 |
| - cur.execute("CREATE TABLE IF NOT EXISTS admin (name TEXT, pass TEXT)") |
35 |
| - |
36 |
| - # Only insert admin if not exists |
37 |
| - cur.execute("SELECT COUNT(*) FROM admin") |
38 |
| - if cur.fetchone()[0] == 0: |
39 |
| - cur.execute("INSERT INTO admin VALUES (?, ?)", ("admin", "admin123")) |
40 |
| - |
41 |
| - conn.commit() |
42 |
| - |
43 |
| - # Fetch last account number to avoid duplicate or incorrect numbering |
44 |
| - cur.execute("SELECT acc_no FROM bank ORDER BY acc_no DESC LIMIT 1") |
45 |
| - acc = cur.fetchone() |
46 |
| - global acc_no |
47 |
| - acc_no = 1 if acc is None else acc[0] + 1 |
48 |
| - |
49 |
| - |
50 |
| -# Check admin details in database |
51 |
| -def check_admin(name, password): |
52 |
| - cur.execute("SELECT * FROM admin WHERE name = ? AND pass = ?", (name, password)) |
53 |
| - return cur.fetchone() is not None |
54 |
| - |
55 |
| - |
56 |
| -# Create employee in database |
57 |
| -def create_employee(name, password, salary, position): |
58 |
| - cur.execute( |
59 |
| - "INSERT INTO staff VALUES (?, ?, ?, ?)", (name, password, salary, position) |
60 |
| - ) |
61 |
| - conn.commit() |
62 |
| - |
63 |
| - |
64 |
| -# Check employee login details |
65 |
| -def check_employee(name, password): |
66 |
| - cur.execute("SELECT 1 FROM staff WHERE name = ? AND pass = ?", (name, password)) |
67 |
| - return cur.fetchone() is not None |
68 |
| - |
69 |
| - |
70 |
| -# Create customer in database |
71 |
| -def create_customer(name, age, address, balance, acc_type, mobile_number): |
72 |
| - global acc_no |
73 |
| - cur.execute( |
74 |
| - "INSERT INTO bank VALUES (?, ?, ?, ?, ?, ?, ?)", |
75 |
| - (acc_no, name, age, address, balance, acc_type, mobile_number), |
76 |
| - ) |
77 |
| - conn.commit() |
78 |
| - acc_no += 1 |
79 |
| - return acc_no - 1 |
80 |
| - |
81 |
| - |
82 |
| -# Check if account number exists |
83 |
| -def check_acc_no(acc_no): |
84 |
| - cur.execute("SELECT 1 FROM bank WHERE acc_no = ?", (acc_no,)) |
85 |
| - return cur.fetchone() is not None |
86 |
| - |
87 |
| - |
88 |
| -# Get customer details |
89 |
| -def get_details(acc_no): |
90 |
| - cur.execute("SELECT * FROM bank WHERE acc_no = ?", (acc_no,)) |
91 |
| - detail = cur.fetchone() |
92 |
| - return detail if detail else False |
93 |
| - |
94 |
| - |
95 |
| -# Update customer balance |
96 |
| -def update_balance(new_money, acc_no): |
97 |
| - cur.execute( |
98 |
| - "UPDATE bank SET balance = balance + ? WHERE acc_no = ?", (new_money, acc_no) |
99 |
| - ) |
100 |
| - conn.commit() |
101 |
| - |
102 |
| - |
103 |
| -# Deduct balance |
104 |
| -def deduct_balance(new_money, acc_no): |
105 |
| - cur.execute("SELECT balance FROM bank WHERE acc_no = ?", (acc_no,)) |
106 |
| - bal = cur.fetchone() |
107 |
| - if bal and bal[0] >= new_money: |
108 |
| - cur.execute( |
109 |
| - "UPDATE bank SET balance = balance - ? WHERE acc_no = ?", |
110 |
| - (new_money, acc_no), |
111 |
| - ) |
112 |
| - conn.commit() |
113 |
| - return True |
114 |
| - return False |
115 |
| - |
116 |
| - |
117 |
| -# Get account balance |
118 |
| -def check_balance(acc_no): |
119 |
| - cur.execute("SELECT balance FROM bank WHERE acc_no = ?", (acc_no,)) |
120 |
| - bal = cur.fetchone() |
121 |
| - return bal[0] if bal else 0 |
122 |
| - |
123 |
| - |
124 |
| -# Update customer details |
125 |
| -def update_name_in_bank_table(new_name, acc_no): |
126 |
| - cur.execute("UPDATE bank SET name = ? WHERE acc_no = ?", (new_name, acc_no)) |
127 |
| - conn.commit() |
128 |
| - |
129 |
| - |
130 |
| -def update_age_in_bank_table(new_age, acc_no): |
131 |
| - cur.execute("UPDATE bank SET age = ? WHERE acc_no = ?", (new_age, acc_no)) |
132 |
| - conn.commit() |
133 |
| - |
134 |
| - |
135 |
| -def update_address_in_bank_table(new_address, acc_no): |
136 |
| - cur.execute("UPDATE bank SET address = ? WHERE acc_no = ?", (new_address, acc_no)) |
137 |
| - conn.commit() |
138 |
| - |
139 |
| - |
140 |
| -def update_mobile_number_in_bank_table(new_mobile_number, acc_no): |
141 |
| - cur.execute( |
142 |
| - "UPDATE bank SET mobile_number = ? WHERE acc_no = ?", |
143 |
| - (new_mobile_number, acc_no), |
144 |
| - ) |
145 |
| - conn.commit() |
146 |
| - |
147 |
| - |
148 |
| -def update_acc_type_in_bank_table(new_acc_type, acc_no): |
149 |
| - cur.execute( |
150 |
| - "UPDATE bank SET account_type = ? WHERE acc_no = ?", (new_acc_type, acc_no) |
151 |
| - ) |
152 |
| - conn.commit() |
153 |
| - |
154 |
| - |
155 |
| -# List all customers |
156 |
| -def list_all_customers(): |
157 |
| - cur.execute("SELECT * FROM bank") |
158 |
| - return cur.fetchall() |
159 |
| - |
160 |
| - |
161 |
| -# Delete account |
162 |
| -def delete_acc(acc_no): |
163 |
| - cur.execute("DELETE FROM bank WHERE acc_no = ?", (acc_no,)) |
164 |
| - conn.commit() |
165 |
| - |
166 |
| - |
167 |
| -# Show employees |
168 |
| -def show_employees(): |
169 |
| - cur.execute("SELECT name, salary, position FROM staff") |
170 |
| - return cur.fetchall() |
171 |
| - |
172 |
| - |
173 |
| -# Get total money in bank |
174 |
| -def all_money(): |
175 |
| - cur.execute("SELECT SUM(balance) FROM bank") |
176 |
| - total = cur.fetchone()[0] |
177 |
| - return total if total else 0 |
178 |
| - |
179 |
| - |
180 |
| -# Get employee details |
181 |
| -def show_employees_for_update(): |
182 |
| - cur.execute("SELECT * FROM staff") |
183 |
| - return cur.fetchall() |
184 |
| - |
185 |
| - |
186 |
| -# Update employee details |
187 |
| -def update_employee_name(new_name, old_name): |
188 |
| - cur.execute("UPDATE staff SET name = ? WHERE name = ?", (new_name, old_name)) |
189 |
| - conn.commit() |
190 |
| - |
191 |
| - |
192 |
| -def update_employee_password(new_pass, old_name): |
193 |
| - cur.execute("UPDATE staff SET pass = ? WHERE name = ?", (new_pass, old_name)) |
194 |
| - conn.commit() |
195 |
| - |
196 |
| - |
197 |
| -def update_employee_salary(new_salary, old_name): |
198 |
| - cur.execute("UPDATE staff SET salary = ? WHERE name = ?", (new_salary, old_name)) |
199 |
| - conn.commit() |
200 |
| - |
201 |
| - |
202 |
| -def update_employee_position(new_pos, old_name): |
203 |
| - cur.execute("UPDATE staff SET position = ? WHERE name = ?", (new_pos, old_name)) |
204 |
| - conn.commit() |
205 |
| - |
206 |
| - |
207 |
| -# Get customer name and balance |
208 |
| -def get_detail(acc_no): |
209 |
| - cur.execute("SELECT name, balance FROM bank WHERE acc_no = ?", (acc_no,)) |
210 |
| - return cur.fetchone() |
211 |
| - |
212 |
| - |
213 |
| -# Check if employee exists |
214 |
| -def check_name_in_staff(name): |
215 |
| - cur.execute("SELECT 1 FROM staff WHERE name = ?", (name,)) |
216 |
| - return cur.fetchone() is not None |
| 79 | + self.conn.commit() |
| 80 | + self.acc_no += 1 |
| 81 | + return acc_no |
| 82 | + |
| 83 | + def check_acc_no(self, acc_no): |
| 84 | + self.cur.execute("SELECT 1 FROM bank WHERE acc_no=?", (acc_no,)) |
| 85 | + return self.cur.fetchone() is not None |
| 86 | + |
| 87 | + def get_details(self, acc_no): |
| 88 | + self.cur.execute("SELECT * FROM bank WHERE acc_no=?", (acc_no,)) |
| 89 | + return self.cur.fetchone() |
| 90 | + |
| 91 | + def get_detail(self, acc_no): |
| 92 | + self.cur.execute("SELECT name, balance FROM bank WHERE acc_no=?", (acc_no,)) |
| 93 | + return self.cur.fetchone() |
| 94 | + |
| 95 | + def update_customer(self, field, new_value, acc_no): |
| 96 | + if field not in {"name", "age", "address", "mobile_number", "account_type"}: |
| 97 | + raise ValueError("Invalid customer field") |
| 98 | + self.cur.execute(f"UPDATE bank SET {field}=? WHERE acc_no=?", (new_value, acc_no)) |
| 99 | + self.conn.commit() |
| 100 | + |
| 101 | + def update_balance(self, amount, acc_no): |
| 102 | + self.cur.execute("UPDATE bank SET balance = balance + ? WHERE acc_no=?", (amount, acc_no)) |
| 103 | + self.conn.commit() |
| 104 | + |
| 105 | + def deduct_balance(self, amount, acc_no): |
| 106 | + self.cur.execute("SELECT balance FROM bank WHERE acc_no=?", (acc_no,)) |
| 107 | + bal = self.cur.fetchone() |
| 108 | + if bal and bal[0] >= amount: |
| 109 | + self.cur.execute("UPDATE bank SET balance=balance-? WHERE acc_no=?", (amount, acc_no)) |
| 110 | + self.conn.commit() |
| 111 | + return True |
| 112 | + return False |
| 113 | + |
| 114 | + def check_balance(self, acc_no): |
| 115 | + self.cur.execute("SELECT balance FROM bank WHERE acc_no=?", (acc_no,)) |
| 116 | + bal = self.cur.fetchone() |
| 117 | + return bal[0] if bal else 0 |
| 118 | + |
| 119 | + def list_all_customers(self): |
| 120 | + self.cur.execute("SELECT * FROM bank") |
| 121 | + return self.cur.fetchall() |
| 122 | + |
| 123 | + def delete_acc(self, acc_no): |
| 124 | + self.cur.execute("DELETE FROM bank WHERE acc_no=?", (acc_no,)) |
| 125 | + self.conn.commit() |
| 126 | + |
| 127 | + # ----------------- Stats ----------------- |
| 128 | + def all_money(self): |
| 129 | + self.cur.execute("SELECT SUM(balance) FROM bank") |
| 130 | + total = self.cur.fetchone()[0] |
| 131 | + return total if total else 0 |
| 132 | + |
| 133 | + # ----------------- Cleanup ----------------- |
| 134 | + def close(self): |
| 135 | + self.conn.close() |
0 commit comments