0% found this document useful (0 votes)
3 views

interface_with_sql_notes[1]

The document provides a Python script using pymysql to create a bank database, including creating a table, inserting records, and displaying or searching for records based on account number, name, or balance. It also includes functionality for deleting records by account number or name. The script demonstrates various database operations with error handling for database transactions.

Uploaded by

amaanramacademic
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

interface_with_sql_notes[1]

The document provides a Python script using pymysql to create a bank database, including creating a table, inserting records, and displaying or searching for records based on account number, name, or balance. It also includes functionality for deleting records by account number or name. The script demonstrates various database operations with error handling for database transactions.

Uploaded by

amaanramacademic
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

To create a bank database

#to create a database

import pymysql

db=pymysql.connect(host="localhost",database=””,user="root",passw
ord="ssc2019")

c=db.cursor()

sql="create database bank;"

c.execute(sql)

db.close()

To create a table in the above created database


#to create a table

import pymysql

db=pymysql.connect("localhost","root","","bank")

c=db.cursor()

sql="""

create table banked(

accno char(15),

name char(20),

bal decimal(10),

mobno char(15),

emailid char(20)); """

c.execute(sql)
db.close()

To Insert record in the above created table


#to take input for details and save in database

import pymysql

db=pymysql.connect("localhost","root","","bank")

c=db.cursor()

a=input("enter accno")

n=input("enter name")

b=float(input("enter balance"))

m=input("enter mobno")

e=input("enter emailid")

try:

sql=("insert into banked (accno,name,bal,mobno,emailid)


values(%s,%s,%s,%s,%s)")

val= (a,n,b,m,e)

c.execute(sql,val)

db.commit()

print("record saved")

except:

db.rollback()

db.close()

Display all the record from the table


#display the records from table

import pymysql

db=pymysql.connect("localhost","root","","bank")

try:

c=db.cursor()

sql='select * from banked;'

c.execute(sql)

print("number of rows: ",countrow)

data=c.fetchall()

for eachrow in data:

print(eachrow)

except:

db.rollback()

db.close()

2nd method to display all the record from the table


#display the records from the table field by field

import pymysql

db=pymysql.connect("localhost","root","","bank")

try:

c=db.cursor()

sql='select * from banked;'

c.execute(sql)
countrow=c.execute(sql)

print("number of rows: ",countrow)

data=c.fetchall()

print("Accno Name Bal Mobno Emailid")

for eachrow in data:

a=eachrow[0]

n=eachrow[1]

b=eachrow[2]

m=eachrow[3]

e=eachrow[4]

print(a," ",n," ",b," ",m," ",e)

except:

db.rollback()

db.close()

To search a record on the basis of accno (account number of


customer)
#search a record by accno field by field

import pymysql

db=pymysql.connect("localhost","root","","bank")

try:

z=0

accno=int(input("Enter accno to search"))


c=db.cursor()

sql='select * from banked;'

c.execute(sql)

print("number of rows: ",countrow)

data=c.fetchall()

for eachrow in data:

a=eachrow[0]

n=eachrow[1]

b=eachrow[2]

m=eachrow[3]

e=eachrow[4]

if(a==accno):

z=1

print(a,n,b,m,e)

if(z==0):

print("Record is not present")

except:

db.rollback()

db.close()

Search a record on the basis of name of the customer


#search a record by name field by field

import pymysql
db=pymysql.connect("localhost","root","","bank")

try:

z=0

name=input("Enter name to search")

c=db.cursor()

sql='select * from banked;'

c.execute(sql)

print("number of rows: ",countrow)

data=c.fetchall()

for eachrow in data:

a=eachrow[0]

n=eachrow[1]

b=eachrow[2]

m=eachrow[3]

e=eachrow[4]

if(n==name):

z=1

print(a,n,b,m,e)

if(z==0):

print("Record is not present")

except:

db.rollback()
db.close()

Delete a record on the basis of accno of customer


#searching a record by accno and deleting it

import pymysql

db=pymysql.connect("localhost","root","ct","bank")

try:

z=0

accno=int(input("Enter accno to search and delete"))

c=db.cursor()

sql='select * from banked;'

c.execute(sql)

countrow=c.execute(sql)

print("number of rows: ",countrow)

data=c.fetchall()

for eachrow in data:

a=eachrow[0]

n=eachrow[1]

b=eachrow[2]

m=eachrow[3]

e=eachrow[4]

if(a==accno):

z=1
print(a,n,b,m,e)

if(z==0):

print("Record is not present")

else:

a=accno

sql1="delete from banked where accno= ",accno

print(sql1)

c.execute(sql1)

db.commit()

except:

db.rollback()

db.close()

Delete a record on the basis of name of customer


#searching a record by name and deleting it

import pymysql

db=pymysql.connect("localhost","root","ct","bank")

try:

z=0

name=input("Enter name to search and delete")

c=db.cursor()

sql='select * from banked;'

c.execute(sql)
countrow=c.execute(sql)

print("number of rows: ",countrow)

data=c.fetchall()

for eachrow in data:

a=eachrow[0]

n=eachrow[1]

b=eachrow[2]

m=eachrow[3]

e=eachrow[4]

if(n==name):

z=1

print(a,n,b,m,e)

if(z==0):

print("Record is not present")

else:

n=name

sql1="delete from banked where name= ",name

print(sql1)

c.execute(sql1)

db.commit()

except:

db.rollback()
db.close()

To search and display records on the basis of balance in


account
#search a record by balance field b field

import pymysql

db=pymysql.connect("localhost","root","ct","bank")

try:

z=0

bal=float(input("Enter balance to search"))

c=db.cursor()

sql='select * from banked;'

c.execute(sql)

print("number of rows: ",countrow)

data=c.fetchall()

for eachrow in data:

a=eachrow[0]

n=eachrow[1]

b=eachrow[2]

m=eachrow[3]

e=eachrow[4]

if(b==bal):

z=1
print(a,n,b,m,e)

if(z==0):

print("Record is not present")

except:

db.rollback()

db.close()

You might also like