Sunday, September 6, 2020

MySQL Python Basic Project Student Record

MYSQL Project 1 Student Record
In [ ]:
def createtable():
    import mysql.connector
    mdb=mysql.connector.connect(host="Localhost",user="root",password="root",db='jnv1')
    myc=mdb.cursor()
    myc.execute('create table student5(name char(40),rollno int)')
    mdb.close()
def insert():
    import mysql.connector
    mdb=mysql.connector.connect(host="Localhost",user="root",password="root",db='jnv1')
    myc=mdb.cursor()
    sql = "INSERT INTO student5 (name, rollno) VALUES (%s, %s)"
    m=input("enter name")
    n=input("rollno")
    val = (m,n)
    myc.execute(sql, val)
    mdb.commit()
    mdb.close()
def showdata():
    import mysql.connector
    mdb=mysql.connector.connect(host="Localhost",user="root",password="root",db='jnv1')
    myc=mdb.cursor()
    myc.execute('select * from student5')
    record=myc.fetchall()
    for x in record:
        print(x[0],x[1])
    mdb.close()
    
while(True):
    print("1.to Create Tale ")
    print("2 to Insert Record ")
    print("3 to Show Record")
    print("4 to Exit")
    ch=int(input("Enter Choice"))
    if(ch==1):
        createtable()
    elif(ch==2):
        insert()
    elif(ch==3):
        showdata()
    elif(ch==4):
        break

No comments:

Post a Comment