0% found this document useful (0 votes)
9 views

SQL Interface to Study.docx

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

SQL Interface to Study.docx

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1.

Write a Python code to connect to a database

import mysql.connector
Mycon=mysql.connector.connect(host=”localhost”,user=”root”,passwd=”tiger”,
database=”project”)
print(mycon)
2. How to create a database in MySQL through Python ?
import mysql.connector
mycon=mysql.connector.connect(host=”localhost”,user=”root”,passwd=”tiger”)
cursor=mycon.cursor( )
cursor.execute(“create database education”)
3. Write the Python code to display the present databases in MySQL.
import mysql.connector
mycon=mysql.connector.connect(host=”localhost”,user=”root”,passwd=”tiger”)
cursor=mycon.cursor()
cursor.execute(“show databases”)
for i in cursor:
print(i)
4. How to create a table in MySQL through Python ?
import mysql.connector
mycon=mysql.connector.connect(host=”localhost”,user=”root”,passwd=”tiger”)
cursor=mycon.cursor()
cursor.execute(“create table student(admn_no int primary key, sname varchar(30), gender
char(2), DOB date, stream varchar(15), marks float”)
5. Write the Python code to insert data into student table of database kvs in MYSQL .
import mysql.connector
mycon=mysql.connector.connect(host=”localhost”,user=”root”,passwd=”tiger”,database
=’kvs’)
cursor=mycon.cursor() ano=int(input(“Enter admission no: “)) n=input(“Enter name: “)
g=input(“Enter gender:”)
dob=input(“Enter DOB: “)
s=input(“Enter stream: “)
m=float(input(“Enter marks:”))
query= “insert into student values( {}, ‘{}’ , ‘{}’ , ’{}’ , ’{}’ , {} )”.format(ano,n,g,dob,s,m)
cursor.execute(query)
mycon.commit( )
6. How to fetch data in Python from a student table of education database in MySQL ?
import mysql.connector
mycon=mysql.connector.connect(host=”localhost”,user=”root”,passwd=”tiger”,
database=”education”)
cursor=mycon.cursor()
cursor.execute(“select * from student”)
for row in cursor:
print(row)

1
7. Write the Python code to update a record in the student table of education database.
import mysql.connector
mycon=mysql.connector.connect(host=”localhost”,user=”root”,passwd=”tiger”,
database=”education”)
cursor=mycon.cursor()
cursor.execute(“update student set marks=67 where admn_no=3456”)
mycon.commit( )
8. Write the Python code to delete a record from the student table of education database
import mysql.connector
mycon=mysql.connector.connect(host=”localhost”,user=”root”,passwd=”tiger”,
database=”education”)
cursor=mycon.cursor()
cursor.execute(“delete from student where admn_no=3455”)
mycon.commit( )

9. Keshav wants to write a program in python to display some records from the CRICKET
Table of SPORTS Database. The MySQL server is running on LOCALHOST and the login
credentials are as following:
Username – keshav
Password – karma
The columns of the CRICKET table are described below:
pid (Player ID) – integer
pname (Player Name) – string
innings (Innings Played) – integer
runs (Runs Scored) – integer
wickets (Wickets taken) – integer
Help him write a program, which will display the name (only name) of all players who have
player 100 or more innings.

import mysql.connector
mycon=mysql.connector.connect(host=”localhost”,user=”keshav”,passwd=”karma”,
database=”sports”)
cursor=mycon.cursor()
cursor.execute(“select pname from cricket where innings>=100”)
for row in cursor:
print(row)

You might also like