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

Chetan1 Mysql and Connecting

The document discusses 5 Python programs that demonstrate connecting to a MySQL database and performing common operations like retrieving data from a table, inserting new records, deleting records, searching within a price range, and fetching details between salary ranges. Each program provides sample code and output to showcase how to connect to a database and execute SQL queries to manipulate data in Python.

Uploaded by

K.D. computer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

Chetan1 Mysql and Connecting

The document discusses 5 Python programs that demonstrate connecting to a MySQL database and performing common operations like retrieving data from a table, inserting new records, deleting records, searching within a price range, and fetching details between salary ranges. Each program provides sample code and output to showcase how to connect to a database and execute SQL queries to manipulate data in Python.

Uploaded by

K.D. computer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

My SQL and connectivity

programs:-

1. Program to display contents of table in


Python

import pymysql as ps

try:

cn=ps.connect(host='localhost',port=3306,user='root',passwor
d='123',db='tata')

cmd=cn.cursor()

query="select * from products"

cmd.execute(query)

rows=cmd.fetchall()

# print(rows)
for row in rows:
for col in row:
print(col,end=' ')
print()

cn.close()

except Exception as e:
print(e)

Output:-
001 macBook Pro 120000 2020
002 iPad Pro 75000 2020

2. Program to illustrate Insertion of records to


database in Python

import pymysql as ps

try:
cn=ps.connect(host='localhost',port=3306,user='root',pa
ssword='123',db='tata')
cmd=cn.cursor()
pid=input("Enter Product Id:")
pn=input("Enter Product Name:")
pr = input("Enter Product Rate:")
md = input("Enter Mf. Date:")
query="insert into products values('{}','{}',
{},'{}')".format(pid,pn,pr,md)
cmd.execute(query)
cn.commit()
cn.close()
print("Record Submitted")
except Exception as e:
print(e)
Output:-
Enter Product Id: 001
Enter Product Name: Sensor 421
Enter Product Rate: 250
Enter Mf. Date: 3.2.2020
Record Submitted

3. Python program to delete a Record from the


Database

import cpymysql as mysql

try:

conn=mysql.connect(host='localhost',port=3306,us
er='root',password='123',db='myschool')
cmd=conn.cursor()

id=input("Enter Faculty Id U Want To Delete:")

q="select * from faculties where


fid='{}'".format(id)
cmd.execute(q)

row=cmd.fetchone()

if(row==None):
print("Not Found")
else:
print("ID:",row[0])
print("Name:", row[1])
print("Birth Date:", row[2])
print("Department:", row[3])
print("Salary:", row[4])

ch=input("Are you Sure(yes/no)?")


if(ch=='yes'):
q="delete from faculties where
fid={}".format(id)
cmd.execute(q)
conn.commit()
print("Record Deleted....")
conn.close()

except Exception as e:
print("Error:",e)

Output:-
Enter Faculty Id U Want To Delete: 03
ID: 03
Name: John
Birth Data: 12.4.1988
Department: computer Science
Salary: 45000
Are you Sure(yes/no)?yes
Record Deleted....

4.Python program to fetch details from the


database
import pymysql as mysql

try:

conn=mysql.connect(host='localhost',port=3306,us
er='root',password='123',db='myschool')
cmd=conn.cursor()

min=input("Enter Min Salary : ")


max=input("Enter Max Salary : ")

q="select * from faculties where salary between {}


and {}".format(min,max)
cmd.execute(q)

rows=cmd.fetchall()

#print(rows)
for row in rows:
print(row[1],row[4])

conn.close()
except Exception as e:
print("Error:",e)

Output:-
Enter Min Salary : 25000
Enter Max Salary : 45000
34 34000
65 29500

5.Program to search a record by price

import pymysql as MYSQL

try:

conn=MYSQL.connect(host='localhost',port=3306,
user='root',password='123',db='practice')

cmd=conn.cursor()

min=input("Enter Min Price?")


max = input("Enter Max Price?")

q="select * from products where productrate


between {} and {}".format(min,max)
cmd.execute(q)

rows=cmd.fetchall()

if(rows==None):
print("Record Not Found...")
else:
for row in rows:
for cols in row:
print(cols,end=' ')
print()

conn.close()
except Exception as e:
print(e)

Output:-

Enter Min Price?5000


Enter Max Price? 10000
04 EarPads 6299 2020
07 wireless Charger 9999 2021

You might also like