11import sqlite3
22
3-
4- # making connection with database
3+ # Making connection with database
54def connect_database ():
65 global conn
76 global cur
87 conn = sqlite3 .connect ("bankmanaging.db" )
9-
108 cur = conn .cursor ()
119
1210 cur .execute (
13- "create table if not exists bank (acc_no int, name text, age int, address text, balance int, account_type text, mobile_number int)"
11+ """
12+ CREATE TABLE IF NOT EXISTS bank (
13+ acc_no INTEGER PRIMARY KEY,
14+ name TEXT,
15+ age INTEGER,
16+ address TEXT,
17+ balance INTEGER,
18+ account_type TEXT,
19+ mobile_number TEXT
20+ )
21+ """
1422 )
1523 cur .execute (
16- "create table if not exists staff (name text, pass text,salary int, position text)"
24+ """
25+ CREATE TABLE IF NOT EXISTS staff (
26+ name TEXT,
27+ pass TEXT,
28+ salary INTEGER,
29+ position TEXT
30+ )
31+ """
1732 )
18- cur .execute ("create table if not exists admin (name text, pass text)" )
19- cur .execute ("insert into admin values('arpit','123')" )
33+ cur .execute ("CREATE TABLE IF NOT EXISTS admin (name TEXT, pass TEXT)" )
34+
35+ # Only insert admin if not exists
36+ cur .execute ("SELECT COUNT(*) FROM admin" )
37+ if cur .fetchone ()[0 ] == 0 :
38+ cur .execute ("INSERT INTO admin VALUES (?, ?)" , ('arpit' , '123' ))
39+
2040 conn .commit ()
21- cur .execute ("select acc_no from bank" )
22- acc = cur .fetchall ()
23- global acc_no
24- if len (acc ) == 0 :
25- acc_no = 1
26- else :
27- acc_no = int (acc [- 1 ][0 ]) + 1
2841
42+ # Fetch last account number to avoid duplicate or incorrect numbering
43+ cur .execute ("SELECT acc_no FROM bank ORDER BY acc_no DESC LIMIT 1" )
44+ acc = cur .fetchone ()
45+ global acc_no
46+ acc_no = 1 if acc is None else acc [0 ] + 1
2947
30- # check admin dtails in database
48+ # Check admin details in database
3149def check_admin (name , password ):
32- cur .execute ("select * from admin" )
33- data = cur .fetchall ()
34-
35- if data [0 ][0 ] == name and data [0 ][1 ] == password :
36- return True
37- return
50+ cur .execute ("SELECT * FROM admin WHERE name = ? AND pass = ?" , (name , password ))
51+ return cur .fetchone () is not None
3852
39-
40- # create employee in database
41- def create_employee (name , password , salary , positon ):
42- print (password )
43- cur .execute ("insert into staff values(?,?,?,?)" , (name , password , salary , positon ))
53+ # Create employee in database
54+ def create_employee (name , password , salary , position ):
55+ cur .execute ("INSERT INTO staff VALUES (?, ?, ?, ?)" , (name , password , salary , position ))
4456 conn .commit ()
4557
46-
47- # check employee details in dabase for employee login
58+ # Check employee login details
4859def check_employee (name , password ):
49- print (password )
50- print (name )
51- cur .execute ("select name,pass from staff" )
52- data = cur .fetchall ()
53- print (data )
54- if len (data ) == 0 :
55- return False
56- for i in range (len (data )):
57- if data [i ][0 ] == name and data [i ][1 ] == password :
58- return True
59-
60- return False
61-
60+ cur .execute ("SELECT 1 FROM staff WHERE name = ? AND pass = ?" , (name , password ))
61+ return cur .fetchone () is not None
6262
63- # create customer details in database
63+ # Create customer in database
6464def create_customer (name , age , address , balance , acc_type , mobile_number ):
6565 global acc_no
6666 cur .execute (
67- "insert into bank values (?,?,?,?,?,?, ?)" ,
68- (acc_no , name , age , address , balance , acc_type , mobile_number ),
67+ "INSERT INTO bank VALUES (?, ?, ?, ?, ?, ?, ?)" ,
68+ (acc_no , name , age , address , balance , acc_type , mobile_number )
6969 )
7070 conn .commit ()
71- acc_no = acc_no + 1
71+ acc_no += 1
7272 return acc_no - 1
7373
74-
75- # check account in database
74+ # Check if account number exists
7675def check_acc_no (acc_no ):
77- cur .execute ("select acc_no from bank" )
78- list_acc_no = cur .fetchall ()
79-
80- for i in range (len (list_acc_no )):
81- if list_acc_no [i ][0 ] == int (acc_no ):
82- return True
83- return False
84-
76+ cur .execute ("SELECT 1 FROM bank WHERE acc_no = ?" , (acc_no ,))
77+ return cur .fetchone () is not None
8578
86- # get all details of a particular customer from database
79+ # Get customer details
8780def get_details (acc_no ):
88- cur .execute ("select * from bank where acc_no=?" , (acc_no ))
89- global detail
90- detail = cur .fetchall ()
91- print (detail )
92- if len (detail ) == 0 :
93- return False
94- else :
95- return (
96- detail [0 ][0 ],
97- detail [0 ][1 ],
98- detail [0 ][2 ],
99- detail [0 ][3 ],
100- detail [0 ][4 ],
101- detail [0 ][5 ],
102- detail [0 ][6 ],
103- )
104-
81+ cur .execute ("SELECT * FROM bank WHERE acc_no = ?" , (acc_no ,))
82+ detail = cur .fetchone ()
83+ return detail if detail else False
10584
106- # add new balance of customer in bank database
85+ # Update customer balance
10786def update_balance (new_money , acc_no ):
108- cur .execute ("select balance from bank where acc_no=?" , (acc_no ,))
109- bal = cur .fetchall ()
110- bal = bal [0 ][0 ]
111- new_bal = bal + int (new_money )
112-
113- cur .execute ("update bank set balance=? where acc_no=?" , (new_bal , acc_no ))
87+ cur .execute ("UPDATE bank SET balance = balance + ? WHERE acc_no = ?" , (new_money , acc_no ))
11488 conn .commit ()
11589
116-
117- # deduct balance from customer bank database
90+ # Deduct balance
11891def deduct_balance (new_money , acc_no ):
119- cur .execute ("select balance from bank where acc_no=?" , (acc_no ,))
120- bal = cur .fetchall ()
121- bal = bal [0 ][0 ]
122- if bal < int (new_money ):
123- return False
124- else :
125- new_bal = bal - int (new_money )
126-
127- cur .execute ("update bank set balance=? where acc_no=?" , (new_bal , acc_no ))
92+ cur .execute ("SELECT balance FROM bank WHERE acc_no = ?" , (acc_no ,))
93+ bal = cur .fetchone ()
94+ if bal and bal [0 ] >= new_money :
95+ cur .execute ("UPDATE bank SET balance = balance - ? WHERE acc_no = ?" , (new_money , acc_no ))
12896 conn .commit ()
12997 return True
98+ return False
13099
131-
132- # gave balance of a particular account number from database
100+ # Get account balance
133101def check_balance (acc_no ):
134- cur .execute ("select balance from bank where acc_no= ?" , (acc_no ))
135- bal = cur .fetchall ()
136- return bal [0 ][ 0 ]
102+ cur .execute ("SELECT balance FROM bank WHERE acc_no = ?" , (acc_no , ))
103+ bal = cur .fetchone ()
104+ return bal [0 ] if bal else 0
137105
138-
139- # update_name_in_bank_table
106+ # Update customer details
140107def update_name_in_bank_table (new_name , acc_no ):
141- print (new_name )
142- conn .execute ("update bank set name='{}' where acc_no={}" .format (new_name , acc_no ))
108+ cur .execute ("UPDATE bank SET name = ? WHERE acc_no = ?" , (new_name , acc_no ))
143109 conn .commit ()
144110
145-
146- # update_age_in_bank_table
147- def update_age_in_bank_table (new_name , acc_no ):
148- print (new_name )
149- conn .execute ("update bank set age={} where acc_no={}" .format (new_name , acc_no ))
111+ def update_age_in_bank_table (new_age , acc_no ):
112+ cur .execute ("UPDATE bank SET age = ? WHERE acc_no = ?" , (new_age , acc_no ))
150113 conn .commit ()
151114
152-
153- # update_address_in_bank_table
154- def update_address_in_bank_table (new_name , acc_no ):
155- print (new_name )
156- conn .execute (
157- "update bank set address='{}' where acc_no={}" .format (new_name , acc_no )
158- )
115+ def update_address_in_bank_table (new_address , acc_no ):
116+ cur .execute ("UPDATE bank SET address = ? WHERE acc_no = ?" , (new_address , acc_no ))
159117 conn .commit ()
160118
161-
162- # list of all customers in bank
119+ # List all customers
163120def list_all_customers ():
164- cur .execute ("select * from bank" )
165- deatil = cur .fetchall ()
166-
167- return deatil
121+ cur .execute ("SELECT * FROM bank" )
122+ return cur .fetchall ()
168123
169-
170- # delete account from database
124+ # Delete account
171125def delete_acc (acc_no ):
172- cur .execute ("delete from bank where acc_no= ?" , (acc_no ))
126+ cur .execute ("DELETE FROM bank WHERE acc_no = ?" , (acc_no , ))
173127 conn .commit ()
174128
175-
176- # show employees detail from staff table
129+ # Show employees
177130def show_employees ():
178- cur .execute ("select name, salary, position,pass from staff" )
179- detail = cur .fetchall ()
180- return detail
181-
131+ cur .execute ("SELECT name, salary, position FROM staff" )
132+ return cur .fetchall ()
182133
183- # return all money in bank
134+ # Get total money in bank
184135def all_money ():
185- cur .execute ("select balance from bank" )
186- bal = cur .fetchall ()
187- print (bal )
188- if len (bal ) == 0 :
189- return False
190- else :
191- total = 0
192- for i in bal :
193- total = total + i [0 ]
194- return total
195-
196-
197- # return a list of all employees name
198- def show_employees_for_update ():
199- cur .execute ("select * from staff" )
200- detail = cur .fetchall ()
201- return detail
136+ cur .execute ("SELECT SUM(balance) FROM bank" )
137+ total = cur .fetchone ()[0 ]
138+ return total if total else 0
202139
140+ # Get employee details
141+ def show_employees_for_update ():
142+ cur .execute ("SELECT * FROM staff" )
143+ return cur .fetchall ()
203144
204- # update employee name from data base
145+ # Update employee details
205146def update_employee_name (new_name , old_name ):
206- print (new_name , old_name )
207- cur .execute ("update staff set name='{}' where name='{}'" .format (new_name , old_name ))
147+ cur .execute ("UPDATE staff SET name = ? WHERE name = ?" , (new_name , old_name ))
208148 conn .commit ()
209149
210-
211150def update_employee_password (new_pass , old_name ):
212- print (new_pass , old_name )
213- cur .execute ("update staff set pass='{}' where name='{}'" .format (new_pass , old_name ))
151+ cur .execute ("UPDATE staff SET pass = ? WHERE name = ?" , (new_pass , old_name ))
214152 conn .commit ()
215153
216-
217154def update_employee_salary (new_salary , old_name ):
218- print (new_salary , old_name )
219- cur .execute (
220- "update staff set salary={} where name='{}'" .format (new_salary , old_name )
221- )
155+ cur .execute ("UPDATE staff SET salary = ? WHERE name = ?" , (new_salary , old_name ))
222156 conn .commit ()
223157
224-
225158def update_employee_position (new_pos , old_name ):
226- print (new_pos , old_name )
227- cur .execute (
228- "update staff set position='{}' where name='{}'" .format (new_pos , old_name )
229- )
159+ cur .execute ("UPDATE staff SET position = ? WHERE name = ?" , (new_pos , old_name ))
230160 conn .commit ()
231161
232-
233- # get name and balance from bank of a particular account number
162+ # Get customer name and balance
234163def get_detail (acc_no ):
235- cur .execute ("select name, balance from bank where acc_no=?" , (acc_no ))
236- details = cur .fetchall ()
237- return details
238-
164+ cur .execute ("SELECT name, balance FROM bank WHERE acc_no = ?" , (acc_no ,))
165+ return cur .fetchone ()
239166
167+ # Check if employee exists
240168def check_name_in_staff (name ):
241- cur = conn .cursor ()
242- cur .execute ("select name from staff" )
243- details = cur .fetchall ()
244-
245- for i in details :
246- if i [0 ] == name :
247- return True
248- return False
169+ cur .execute ("SELECT 1 FROM staff WHERE name = ?" , (name ,))
170+ return cur .fetchone () is not Non
0 commit comments