You are on page 1of 4

#Write a program in python to make connectivity between mysql and python (database & table already

created) and update a record.


#input and show data from an already created table in sql
import mysql.connector as c
db=c.connect(host='localhost',user='root', passwd='admin',database='school')
print(db)
cur = db.cursor()

cur.execute("select * from student")


for i in cur:
print (i)

print("\nEnter the rollno to be updated :")


r = int(input("enter the rollno:="))

sql="select * from student where rn = %s " %(r,)


cur.execute(sql)
for i in cur:
print (i)

print("\nEnter the values for student table:")


n = input("enter the name:=")
val=(n,r)
sql="update student set name = %s where rn = %s"
cur.execute(sql,val)
db.commit()
print("sucess")

cur.execute("select * from student")


for i in cur:
print (i)
#Write a program in python to make connectivity between mysql and python (database & table already
created) and delete a record.
#input and show data from an already created table in sql
import mysql.connector as c
db=c.connect(host='localhost',user='root', passwd='admin',database='school')
print(db)
cur = db.cursor()

cur.execute("select * from student")


for i in cur:
print (i)

print("\nEnter the rollno to be deleted :")


r = int(input("enter the rollno:="))

sql="delete from student where rn = %s " %(r,)


cur.execute(sql)
db.commit()
print("sucess")

cur.execute("select * from student")


for i in cur:
print (i)
#Write a program in python to make connectivity between mysql and python (database & table already
created) and insert a record.
#input and show data from an already created table in sql
import mysql.connector as c
db=c.connect(host='localhost',user='root', passwd='root',database='school')
print(db)
cur = db.cursor()
#cur.execute("show databases")
#val = (1,'ram',92)
sql="insert into student values(%s,%s,%s, %s)"
r=int(input("enter roll no::"))
n=input("Enter name::")
p=int(input("Enter %"))
city=input("Enter city:")
val=(r,n,p,city)
cur.execute(sql,val)
db.commit()
print("sucess")

cur.execute("select * from student")


for i in cur:
print (i)
#Write a program in python to make connectivity between mysql and python (creation of database &
table) and insert a record.
# first time creation of database and table
# enter and show one row in table
import mysql.connector as c
db=c.connect(host='localhost',user='root', passwd='root')
cur = db.cursor()
cur.execute("show databases")
print(cur)
for i in cur:
print(i)

cur.execute("create database school")


print("\nnew database is created")
cur.execute("show databases")
print(cur)
for i in cur:
print(i)

cur.execute("use school")
cur.execute("create table student(rn int, name char(15), per int)")
cur.execute("describe student")
print("\nthe structure of table student is:")
for i in cur:
print(i)

print("\nEnter the values for student table:")


r = int(input("enter the rollno:="))
n = input("enter the name:=")
p = int(input("enter the percentage"))
#val = (1,"ram",92)
val=(r,n,p)
sql="insert into student values(%s,%s,%s)"
cur.execute(sql,val)
db.commit()
print("sucess")

cur.execute("select * from student")


for i in cur:
print (i)

You might also like