English

The code given below inserts the following record in the table Student: RollNo - integer Name - string Clas - integer Marks - integer Note the following to establish connectivity between - Computer Science (Python)

Advertisements
Advertisements

Question

The code given below inserts the following record in the table Student:

RollNo - integer
Name - string
Clas - integer
Marks - integer

Note the following to establish connectivity between Python and MYSQL:

  • Username is root
  • Password is tiger
  • The table exists in an MYSQL database named school.
  • The details (RollNo, Name, Clas and Marks) are to be accepted by the user.

Write the following missing statements to complete the code:

Statement 1 - to form the cursor object.

Statement 2 - to execute the command that inserts the record in the table Student.

Statement 3 - to add the record permanently to the database.

import mysql.connector as mysql
def sql_data():
        con1=mysql.connect(host="localhost",user="root",password="tiger", database="school")
 mycursor=_________________ #Statement 1 
 rno=int(input("Enter Roll Number :: "))
 name=input("Enter name :: ")
 clas=int(input("Enter class :: "))
 marks=int(input("Enter Marks :: "))
 querry="insert into student 
values({},'{}',{},{})".format(rno,name,clas,marks)
--------------------#Statement 2
--------------------# Statement 3
 print("Data Added successfully")
Short Note

Solution

Statement 1:
con1.cursor()

Statement 2:
mycursor.execute(querry)

Statement 3:
con1.commit()
shaalaa.com
Performing Insert, Update, Delete Queries Using Cursor
  Is there an error in this question or solution?
2022-2023 (March) Sample

APPEARS IN

Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×