0% found this document useful (0 votes)
6 views6 pages

SQL Python Database Operations Guide

The document contains Python programs for connecting to a MySQL database and performing operations such as printing all databases, showing tables in a specific database, and a menu-driven program for inserting, deleting, and displaying records in a 'teachers' table. It demonstrates the use of the mysql.connector library to interact with the database. The programs include user input for various operations and commit changes to the database accordingly.

Uploaded by

aswalayush12
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)
6 views6 pages

SQL Python Database Operations Guide

The document contains Python programs for connecting to a MySQL database and performing operations such as printing all databases, showing tables in a specific database, and a menu-driven program for inserting, deleting, and displaying records in a 'teachers' table. It demonstrates the use of the mysql.connector library to interact with the database. The programs include user input for various operations and commit changes to the database accordingly.

Uploaded by

aswalayush12
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

SQL PYTHON CONNECTIVITY PROGRAM

1) PROGRAM TO PRINT ALL DATABASES IN PYTHON

import [Link] as sql

mydb=[Link](host="localhost",user="root",passwd="root",database="compeny")

myc=[Link](buffered=True)

[Link]("show databases")

for i in myc:

print(i)

OUTPUT
2) PROGRAM TO SHOW ALL TABLES IN DATABASE COMPENY

import [Link] as sql

mydb=[Link](host="localhost",user="root",passwd="root",database="compeny")

myc=[Link](buffered=True)

[Link]("show tables")

for i in myc:

print(i)
3) MENU DRIVEN PROGRAM TO PERFORM DIFFERENT OPERATION LIKE INSERT,
DELETE,SHOW IN TABLE TEACHERS OF DATABASE COMPENY

import [Link] as sql


mydb=[Link](host="localhost",user="root",passwd="root",database="compeny")
myc=[Link]()
c='y'
while c=='y':
print("1. Insert")
print("2. Delete:")
print("3. show:")
print("4. Exit")
ch=int(input("Enter choice"))
if ch==1:
tid=input("enter tid")
tnam=input("enter teacher name")
tinsert="insert into teachers(tid,tname) values(%s,%s)"
r=(tid,tnam)
[Link](tinsert,r)

[Link]()
elif ch==2:
td=input("enter id for delete")
r=(td,)
tdel="delete from teachers where tid=%s"
[Link](tdel,r)
print("record deleted")
[Link]()
elif ch==3:
[Link]("select * from teachers")
for i in myc:
print(i)
else:
break

OUTPUT
SQL QUERIES BASED ON TABLE CLASS AND SPORT

You might also like