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

Python Mysql Interface Program

The document shows code to create a database and table in MySQL using Python. It then inserts two records into the table and queries the table to display records where age is less than or equal to 15.

Uploaded by

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

Python Mysql Interface Program

The document shows code to create a database and table in MySQL using Python. It then inserts two records into the table and queries the table to display records where age is less than or equal to 15.

Uploaded by

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

# Write a program using Python Mysql Conectivity to Create

# a Database(School) at First then create table student


# (Roll INT(4) primary key,Name VARCHAR(20),Age INT(2),
# City VARCHAR(20),Marks INT(4))
# Store any 2 record in it and then search and display only
# those records in which age<=15.

import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="hima
nshu")
mycursor=mydb.cursor()
mycursor.execute("CREATE DATABASE School")
mydb=mysql.connector.connect(host="localhost",user="root",passwd="hima
nshu",database="School")
mycursor=mydb.cursor()
mycursor.execute("CREATE TABLE student (Roll INT(4) primary key,Name
VARCHAR(20),Age INT(2),City VARCHAR(20),Marks INT(4))")
sql="INSERT INTO student (Roll,Name,Age,City,Marks) values
(%s,%s,%s,%s,%s)"
val=(101,'Pankaj',15,'Barabanki',300)
mycursor.execute(sql,val)
sql="INSERT INTO student (Roll,Name,Age,City,Marks) values
(%s,%s,%s,%s,%s)"
val=(102,'Anuj',16,'Lucknow',290)
mycursor.execute(sql,val)
mydb.commit()
mycursor.execute("select * from student where age<=15")
myresult=mycursor.fetchall()
for x in myresult:
print(x)

You might also like