Interface 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

PROGRAM 21:

INTERFACE MYSQL WITH PYTHON –I-(INSERT RECORDS)


Q21. Create table Students in MySQL and insert values in it and display the content using Python.

AIM:- To create table students in MySQL and insert values in it and display the content using Python.

ALGORITHM:

Step1: Start the process

Step2: Import the mysql.connector” module which is used to connect MySQL database with in
Python.

Step3:- Create a variable and specify the ‘host’, ‘user’, ‘password’ and ‘database’.

Step 4: Specify another variable and create an instance of cursor by using ‘cursor()’.

Step 5: Now, create a table specify the table name “Students” and the required fields necessary and
execute.

Step 6: Specify a variable and insert a single value or multiple values using while loop in the table.

Step 7:- Execute the query using execute() function and followed by commit() function.

Step 8:- Fetch the records from the table in MySQL and print it .

Step 9:- Stop the process

PROGRAM:

import mysql.connector as ms
mycon=ms.connect(host="localhost",user="root",passwd="12345",
database="mysql")
if mycon.is_connected()==True:
print ("sucess")
cursor=mycon.cursor()

cursor.execute("CREATE TABLE IF NOT EXISTS STUDENTS (SNO


INT,NAME CHAR(25),MARKS FLOAT,GRADE CHAR(1),PROJECT
CHAR(25))")
# creating table an inserting only one data
print(“Insert single record in the table”)
st= "INSERT INTO STUDENTS (SNO, NAME,
PROGRAM MARKS,GRADE,PROJECT)VALUES
({},'{}',{},'{}','{}')".format(1,'Ravi',75.5,'B',"Submitted")
cursor.execute(st)
#fetching one data from mysql and printing
cursor.execute("select * from STUDENTS")
t=cursor.fetchone()
print(t)
mycon.commit()

# creating table an inserting many data


print(“Insert many records in the table”)

ans='y'
while ans=='y':
e=int(input("ENTER student NO."))
n=input("enter Name:")
m=float(input("enter the marks"))
g=input("enter grade")
p= input("project")
st="insert into students
values({},'{}',{},'{}','{}')".format(e,n,m,g,p)
cursor.execute(st)
mycon.commit()
ans=input("enter whether u want to continue(y/n)")
#fetching one data from mysql and printing
cursor.execute("select * from STUDENTS")
t=cursor.fetchall()
for i in t:
print(i)
mycon.commit()
mycon.close

OUTPUT:- Insert single record in the table


sucess
(1, 'Ravi', 75.5, 'B', 'Submitted')

Insert many records in the table


sucess
ENTER student NO. 2
enter Name: RADHA
enter the marks 56.5
enter grade C
project NOT SUBMITTED
enter whether u want to continue(y/n)y

ENTER student NO.3


enter Name: GITA
enter the marks 80
enter grade B
project SUBMITTED
enter whether u want to continue(y/n)n

(1, 'Ravi', 75.5, 'B', 'Submitted')


(2, 'RADHA', 56.5, 'C', 'NOT SUBMITTED')
(3, 'GITA', 80.0, 'B', 'SUBMITTED')

OUTPUT: SINGLE RECORD INSERTED

MANY RECORDS INSERTED


PROGRAM 22:
INTERFACE MYSQL WITH PYTHON –II-(SEARCH RECORD)
Q22. Create table employee, search and print the records from MySQL using Python.

AIM:- To create table Employee, search and print the records from MySQL using Python

ALGORITHM:

Step1: Start the process

Step2: Import the mysql.connector” module which is used to connect MySQL database with in
Python.

Step3:- Create a variable and specify the ‘host’, ‘user’, ‘password’ and ‘database’.

Step 4: Specify another variable and create an instance of cursor by using ‘cursor()’.

Step 5: Now, create a table specify the table name “Emp” and the required fields necessary and
execute.

Step 6: Insert multiple values using while loop in the table.

Step 7:- Execute the query using execute() function and followed by commit() function.

Step 8:- search for particular record or records according the query given , fetch and print it

Step 9:- Stop the process

PROGRAM:-
import mysql.connector as ms
mycon=ms.connect(host="localhost",user="root",passwd="12345",
database="neela")
cursor=mycon.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS EMP(eno int,ename
varchar(30), dept char(25), DOJ date, salary int)")
ans='y'
while ans=='y':
e=int(input("ENTER EMPLOYEE NO."))
n=input("enter Name:")
dp=input("enter the department")
doj=input("enter of DoJ")
s= int(input("enter the salary"))
query="insert into emp
values({},'{}','{}','{}',{})".format(e,n,dp,doj,s)
cursor.execute(query)
mycon.commit()
ans=input("enter whether u want to continue(y/n)")
#fetching data and printing
cursor.execute("select * from emp")
t=cursor.fetchall()
for i in t:
print(i)

#search and print particular field from the table


print("SEARCH OF DATA WITH ONE CONSTRAINT FROM THE TABLE")
e=int(input("Enter the Employee no. to search :"))
cursor.execute("SELECT * FROM EMP WHERE ENO={}".format(e,))
#fetching data and printing
cd=cursor.fetchone()
print(cd)

print("SEARCH OF DATA WITH MORE THAN ONE CONSTRAINTS FROM THE


TABLE")
d=input("Enter department")
cursor.execute("SELECT * FROM EMP WHERE SALARY>50000 AND
DEPT='{}'".format(d,))
#fetching data and printing
cd=cursor.fetchall()
for i in cd:
print(i)
mycon.commit()

OUTPUT
ENTER EMPLOYEE NO.1
enter Name:RITA
enter the departmentSALES
enter of DoJ12/09/16
enter the salary56000
enter whether u want to continue(y/n)y

ENTER EMPLOYEE NO.2


enter Name:HARISH
enter the departmentIT
enter of DoJ22/12/10
enter the salary34000
enter whether u want to continue(y/n)y
ENTER EMPLOYEE NO.3
enter Name:BINA
enter the departmentSALES
enter of DoJ15/12/25
enter the salary25000
enter whether u want to continue(y/n)y

ENTER EMPLOYEE NO.4


enter Name:JINA
enter the departmentSALES
enter of DoJ21/05/29
enter the salary40000
enter whether u want to continue(y/n)y

ENTER EMPLOYEE NO.5


enter Name:YAMUNA
enter the departmentSALES
enter of DoJ14/07/22
enter the salary78000
enter whether u want to continue(y/n)n

(1, 'RITA', 'SALES', datetime.date(2012, 9, 16), 56000)


(2, 'HARISH', 'IT', datetime.date(2022, 12, 10), 34000)
(3, 'BINA', 'SALES', datetime.date(2015, 12, 25), 25000)
(4, 'JINA', 'SALES', datetime.date(2021, 5, 29), 40000)
(5, 'YAMUNA', 'SALES', datetime.date(2014, 7, 22), 78000)

SEARCH OF DATA WITH ONE CONSTRAINT FROM THE TABLE


Enter the Employee no. to search :9
None

SEARCH OF DATA WITH MORE THAN ONE CONSTRAINTS FROM THE TABLE
Enter department SALES
(1, 'RITA', 'SALES', datetime.date(2012, 9, 16), 56000)
(5, 'YAMUNA', 'SALES', datetime.date(2014, 7, 22), 78000)
OUTPUT :Records in the Emp and result of search
PROGRAM 23:
INTERFACE MYSQL WITH PYTHON –III-(UPDATE)
Q23. Create table Employee, insert and update the record in MySQL using python.
AIM:- To Create table Employee, insert and update the record in MySQL using Python.

ALGORITHM:

Step1: Start the process

Step2: Import the mysql.connector” module which is used to connect MySQL database with in
Python.

Step3:- Create a variable and specify the ‘host’, ‘user’, ‘password’ and ‘database’.

Step 4: Specify another variable and create an instance of cursor by using ‘cursor()’.

Step 5: Now, create a table specify the table name “Emp” and the required fields necessary and
execute.

Step 6: insert multiple values using while loop in the table.

Step 7:- Execute the query using execute() function and followed by commit() function.

Step 8: Update the specific record from the table

Step 9:- Fetch the records from the table in MySQL and print it .

Step 10:- Stop the process

PROGRAM:

import mysql.connector as ms
mycon=ms.connect(host="localhost",user="root",passwd="12345",d
atabase="neela")
cursor=mycon.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS EMP(eno int,ename
varchar(30), dept char(25), DOJ date, salary int)")
ans='y'
while ans=='y':
e=int(input("ENTER EMPLOYEE NO."))
n=input("enter Name:")
dp=input("enter the department")
doj=input("enter of DoJ")
s= int(input("enter the salary"))
query="insert into emp
values({},'{}','{}','{}',{})".format(e,n,dp,doj,s)
cursor.execute(query)
mycon.commit()
ans=input("enter whether u want to continue(y/n)")
#fetching data and printing
cursor.execute("select * from emp")
t=cursor.fetchall()
for i in t:
print(i)

#update one feild in the table


print("UPDATION OF DATA")
e=int(input("Employee no. to be updated"))
s=int(input("Enter new salary"))
cursor.execute("update emp set salary=%s where eno=%s"%(s,e))

#fetching data and printing


cursor.execute("select * from emp")
cd=cursor.fetchall()
for i in cd:
print(i)
mycon.commit()

OUTPUT

ENTER EMPLOYEE NO.7


enter Name:Nimal
enter the departmentPurchase
enter of DoJ20/09/15
enter the salary56050
enter whether u want to continue(y/n)n
(1, 'Devi', 'sales', datetime.date(2012, 12, 20), 20000)
(2, 'prajeet', 'hr', datetime.date(2012, 12, 20), 40000)
(3, 'Hari', 'Computer', datetime.date(2004, 1, 12), 60000)
(4, 'ferad', 'purchase', datetime.date(2006, 4, 13), 78999)
(5, 'john', 'sales', datetime.date(2015, 7, 25), 45000)
(6, 'Raju', 'Computer', datetime.date(2018, 12, 29), 55000)
(7, 'Nimal', 'Purchase', datetime.date(2020, 9, 15), 56050)

UPDATION OF DATA
Employee no. to be updated4
Enter new salary80000
(1, 'Devi', 'sales', datetime.date(2012, 12, 20), 20000)
(2, 'prajeet', 'hr', datetime.date(2012, 12, 20), 40000)
(3, 'Hari', 'Computer', datetime.date(2004, 1, 12), 60000)
(4, 'ferad', 'purchase', datetime.date(2006, 4, 13), 80000)
(5, 'john', 'sales', datetime.date(2015, 7, 25), 45000)
(6, 'Raju', 'Computer', datetime.date(2018, 12, 29), 55000)
(7, 'Nimal', 'Purchase', datetime.date(2020, 9, 15), 56050)
OUTPUT: DESC OF EMP TABLE

BEFORE UPDATING: TABLE EMP

AFTER UDATING: TABLE EMP


PROGRAM 24:
INTERFACE MYSQL WITH PYTHON –IV-(ALTER TABLE)
Q24. Create a table Product, alter it, enter the records and print only 2 records from
MySQL using Python.
AIM:- To Create a table Product, alter it , enter the records and print only 2 records from
MySQL using Python.
ALGORITHM:

Step1: Start the process

Step2: Import the mysql.connector” module which is used to connect MySQL database with in
Python.

Step3:- Create a variable and specify the ‘host’, ‘user’, ‘password’ and ‘database’.

Step 4: Specify another variable and create an instance of cursor by using ‘cursor()’.

Step 5: Now, create a table specify the table name “Product” and the required fields necessary and
execute.

Step 6: Alter the table by adding two more fields in it.

Step 7: insert multiple values using while loop in the table.

Step 8:- Execute the query using execute() function and followed by commit() function.

Step 9:- Fetch the records from the table in MySQL and print it .

Step 10:- Stop the process

PROGRAM:

import mysql.connector as ms
mycon=ms.connect(host="localhost",user="root",passwd="12345",
database="mysql")
if mycon.is_connected()==True:
print ("Sucessfully connected")
cursor=mycon.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS PRODUCT (PNO INT
PRIMARY KEY,NAME CHAR(25),PRICE FLOAT)")
cursor.execute("DESC PRODUCT")
t=cursor.fetchall()
for i in t:
print(i)

print("\nALTER TABLE BY ADDING TWO MORE FIELDS")


cursor.execute("ALTER TABLE PRODUCT ADD(QUALITY INT, D_O_ORD
DATE)")
cursor.execute("DESC PRODUCT")
alt=cursor.fetchall()
for i in alt:
print(i)

ans='y'
while ans=='y':
pn=int(input("Enter Product No. :"))
n=input("Enter PName :")
p=float(input("Enter the price :"))
q=int(input("Enter no. of quality :"))
d= input("Date of Order :")
st="insert into product
values({},'{}',{},{},'{}')".format(pn,n,p,q,d)
cursor.execute(st)
mycon.commit()
ans=input("enter whether u want to continue(y/n)")

cursor.execute("select * from product")


ft=cursor.fetchmany(2)
for i in ft:
print(i)

OUTPUT
successfully connected
('PNO', b'int', 'NO', 'PRI', None, '')
('NAME', b'char(25)', 'YES', '', None, '')
('PRICE', b'float', 'YES', '', None, '')

ALTER TABLE BY ADDING TWO MORE FIELDS


('PNO', b'int', 'NO', 'PRI', None, '')
('NAME', b'char(25)', 'YES', '', None, '')
('PRICE', b'float', 'YES', '', None, '')
('QUALITY', b'int', 'YES', '', None, '')
('D_O_ORD', b'date', 'YES', '', None, '')

Enter Product No. :4


Enter PName :HAIR OIL
Enter the price :50
Enter no. of quality :1
Date of Order :21/03/10
enter whether u want to continue(y/n)y
Enter Product No. :2
Enter PName :SOAP
Enter the price :40
Enter no. of quality :2
Date of Order :21/10/08
enter whether u want to continue(y/n)y

Enter Product No. :1


Enter PName :RIN POWDER
Enter the price :320
Enter no. of quality :2
Date of Order :21/09/18
enter whether u want to continue(y/n)n

(1, 'RIN POWDER', 320.0, 2, datetime.date(2021, 9, 18))


(2, 'SOAP', 40.0, 2, datetime.date(2021, 10, 8))

OUTPUT:

DESC PRODUCT TABLE

ALTER TABLE PRODUCT(ADDING TWO MORE FIELDS QUALITY AND D_O_ORD)


RECORDS IN PRODUCT
PROGRAM 25:
INTERFACE MYSQL WITH PYTHON-V(DELETE )
Q25. Create table Employee, insert data and deleting the record in MySQL using Python.

AIM:- To create table employee, , insert data and deleting the record in MySQL using Python

ALGORITHM:

Step1: Start the process

Step2: Import the mysql.connector” module which is used to connect MySQL database with in
Python.

Step3:- Create a variable and specify the ‘host’, ‘user’, ‘password’ and ‘database’.

Step 4: Specify another variable and create an instance of cursor by using ‘cursor()’.

Step 5: Now, create a table specify the table name “Emp” and the required fields necessary and
execute.

Step 6: insert multiple values using while loop in the table.

Step 7:- Execute the query using execute() function and followed by commit() function.

Step 8: Delete the specific record from the table

Step 9:- Fetch the records from the table in MySQL and print it .

Step 10:- Stop the process

PROGRAM:-
import mysql.connector as ms
mycon=ms.connect(host="localhost",user="root",passwd="12345",
database="neela")
cursor=mycon.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS EMP(eno int,ename
varchar(30), dept char(25), DOJ date, salary int)")
ans='y'
while ans=='y':
e=int(input("ENTER EMPLOYEE NO."))
n=input("enter Name:")
dp=input("enter the department")
doj=input("enter of DoJ")
s= int(input("enter the salary"))
query="insert into emp
values({},'{}','{}','{}',{})".format(e,n,dp,doj,s)
cursor.execute(query)
mycon.commit()
ans=input("enter whether u want to continue(y/n)")

#fetching data and printing


cursor.execute("select * from emp")
t=cursor.fetchall()
for i in t:
print(i)

#delete one field in the table


print("DELETING OF DATA FROM THE TABLE")
e=int(input("Enter the Employee no. to be deleted :"))
cursor.execute("DELETE FROM EMP WHERE ENO={}".format(e,))
mycon.commit()

#fetching data and printing


cursor.execute("select * from emp")
cd=cursor.fetchall()
for i in cd:
print(i)
mycon.commit()

OUTPUT

ENTER EMPLOYEE NO.4


enter Name:BINA
enter the departmentSALES
enter of DoJ21/09/18
enter the salary77000
enter whether u want to continue(y/n)n
(1, 'RADHA', 'SALES', datetime.date(2020, 12, 16), 25000)
(2, 'SITA', 'PURCHASE', datetime.date(2012, 3, 24), 45000)
(3, 'GITA', 'IT', datetime.date(2020, 2, 9), 55000)
(5, 'Devi', 'II', datetime.date(2019, 9, 18), 44000)
(4, 'BINA', 'SALES', datetime.date(2021, 9, 18), 77000)
DELETING OF DATA FROM THE TABLE
Enter the Employee no. to be deleted : 5
(1, 'RADHA', 'SALES', datetime.date(2020, 12, 16), 25000)
(2, 'SITA', 'PURCHASE', datetime.date(2012, 3, 24), 45000)
(3, 'GITA', 'IT', datetime.date(2020, 2, 9), 55000)
(4, 'BINA', 'SALES', datetime.date(2021, 9, 18), 77000)

OUTPUT: MySQL
TABLE BEFORE DELETION:
TABLE AFTER DELETION:

You might also like