Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

import mysql.

connector
try:
dataBase = mysql.connector.connect(host="localhost",user="root",
passwd="Viji@2021")
print("Database Connected")
cursorObject = dataBase.cursor()
cursorObject.execute("CREATE DATABASE STUDENT")
print("Database Created")
except Exception as ex:
print(ex)

try:
#connect to the newly created or existing database
dataBase = mysql.connector.connect(host ="localhost",
user="root",
passwd="Viji@2021",
database="Student")
cursorObject = dataBase.cursor()

# creating table
studentrecord = """CREATE TABLE studentdetails(
srn_no varchar(3) primary key,
sname varchar(20) NOT NULL,
course_name varchar(50),
mobile varchar(10),
city varchar(30)
)"""
#execute the query
cursorObject.execute(studentrecord)

print("Table Created ")


except Exception as ex:
print(ex)

try:
ch='y'
dataBase = mysql.connector.connect(host='localhost',user='root',
password='Viji@2021',
database='Student')
cursorObject = dataBase.cursor()
while ch=='y' or ch == 'Y':
sql="insert into
studentdetails(srn_no,sname,course_name,mobile,city)values(%s,%s,%s,%s,%s)"
srn_no = input("enter the SRN_No:")
sname = input("enter the Student Name:")
course_name = input("enter the Course Name:")
mobile = input("enter the Mobile No:")
city = input("enter the City:")
val=(srn_no,sname,course_name,mobile,city)
cursorObject.execute(sql,val)
dataBase.commit()
print("Record Inserted")
ch=input("Do you want to continue y/n:")
except Exception as ex:
print("Error inserting record",ex)

You might also like