Friday, 22 January 2021

Practical - 24: Python MySql Connectivity for class 12 CS and IP

 


Practical - 24: Python MySql Connectivity for class 12 CS and IP

 '''Write a menu driven program to demonstrate add, display, update, delete and exit.

Performed on a table movie containing (mid, mname, rating) through

python-MySql connectivity.'''




import mysql.connector

con=mysql.connector.connect(host="localhost",username="root",passwd="root")

mycursor=con.cursor()

mycursor.execute("create database if not exists spsharmag")

mycursor.execute("use spsharmag")


mycursor.execute("create table if not exists movie (mid int primary key,mname varchar(20),rating float(5,2))")


c="y"


while(c=="y" or c=="Y"):


    print("1. Press 1 for add new movie: ")


    print("2. Press 2 for Show the details of movie: ")


    print("3. Press 3 for Update movie Details: ")


    print("4. Press 4 for Delete movie Details: ")


    print("5. Press 5 for Exit: ")


    choice=int(input("Enter Your Choice 1 or 2 or 3 or 4 or 5: "))


    if(choice==1):


        mid=int(input("Enter movie Id: "))


        mname=input("Enter movie Name: ")


        rating=float(input("Enter movie rating: "))


        mycursor.execute("insert into movie values(%s,%s,%s)",(mid,mname,rating))


        con.commit()


    elif(choice==2):


        mycursor.execute("select * from movie")


        mymovies=mycursor.fetchall()


        for x in mymovies:


            print(x)


    elif(choice==3):


        mid=int(input("Enter the movie id for update: "))


        mname=input("Enter movie New Name: ")


        rating=float(input("Enter movie New Rating: "))


        mycursor.execute("update movie set mname=%s,rating=%s where mid=%s",(mname,rating,mid))


        con.commit()


    elif(choice==4):


        mid=int(input("Enter the movie id for delete: "))


        mycursor.execute("delete from movie where mid=%s",(mid,))


        con.commit()


    elif(choice==5):


        break


    else:


        print("Wrong Choice")


    c=input("Press 'y' for continue and 'n' for exit: ")







No comments:

Post a Comment