Advertisements
Advertisements
Question
Kabir wants to write a program in Python to insert the following record in the table named Student in MYSQL database,
SCHOOL:
- rno(Roll number) - integer
- name(Name) - string
- DOB (Date of birth) - Date
- Fee - float
Note the following to establish connectivity between Python and MySQL:
- Username - root
- Password - tiger
- Host - localhost
The values of fields rno, name, DOB and fee has to be accepted from the user. Help Kabir to write the program in Python.
Short/Brief Note
Advertisements
Solution
import mysql.connector as mysql
con1 = mysql.connect (host="localhost", user="root", password="tiger", database="sample2023")
mycursor=con1.cursor()
rno = int (input ("Enter Roll Number:: "))
name = input(*Enter the name:: ")
DOB = input ("Enter date of birth::")
fee= float (input ("Enter Fee:: "))
query = "INSERT into student values{{},'{}','{}',{})".format (rno, name, DOB, fee)
mycursor.execute (query)
con1.commit()
print ("Data added successfully")
con1.close()shaalaa.com
Data Types and Constraints in MySQL
Is there an error in this question or solution?
