You are on page 1of 15

EARITHMETIC CALCULATOR

AIM
To write a to make
program a
simple Arithmetic Calculator
ALGORITHM:
Step 1 GETthe option from the user for +, - .*,/,/1,%
Step 2 Perform arithmetic operation based on user choice
Step 3 Display the output

CODING:
# define functions
def add(x, y):
return x +y
def subtract(x, y):
returnx-y
def multiply(x, y):
return x *y
def divide(x, y):
return x /y
def floordiv(x, y):
return x y
def moddiv(x, y):
return x %y
#take input from the user
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
print("5.Floor Division")
print("6.Modulo Division")
choice input("Enter choice(1/2/3/4/5/6):")
while choice<7':
numl int(input("Enter first number: ")
num2 int(input("Enter second number: "))
ifchoice='1':
print(num1,"+"num2,"=", add(numl,num2))
elif choice=2:
print(num1,""num2,"=", subtract(numl,num2))
elif choice='3:
print(num1,"*"num2,"=", multiply(num1,num2))
elif choice== 4':
print(num1,"/",num2,"=", divide(num1,num2))
elif choice="5:
print(num1,"//",num2,"=",floordiv(numl,num2))
elif choice=6':
print(num1,"%",num2,"=",moddiv(numl,num2))
choice = input("Enter choice(1/2/3/4/5/6):")

print("Exited")

OUTPUT:
Select operation.
1.Add
2.Subtract
3.Multiply
4.Divide
5.Floor Division
6.Modulo Division
Enter choice(1/2/3/4/5/6):1
Enter first number: 10
Enter second number: 20
10+ 20 30
Enter choice(1/2/3/4/5/6):2
Enter first number: 30
Enter second number: 15
30 -15 15
Enter choice(1/2/3/4/5/6):3
Enter first number: 30
Enter second number: 10
30 * 1 0 = 300

Enter choice(1/2/3/4/5/6):4
Enter first number: 30
Enter second number: 4
30/4 7.5
Enter choice(1/2/3/4/5/6):5
Enter first number: 30
Enter second number: 4
30//4 7
Enter choice(1/2/3/4/5/6):6
Enter first number: 30
Enter second number: 4
30 % 4 2
Enter choice(1/2/3/4/5/6):7
Exited
>>>

RESULT:
Thus the program has been executed successfully for designing a simple arithmetic
calculator and output was verified

16.SQL COMMANDS FOR THE RELATION STUDENT


Aim:
To write SQL commands for the following on the basis of given table student.
STUDENT

No. Name Dept Admn Age Fee Gender

1 akesh Computer | 20/02/2000 23 4000 M

2 Jeni History 10/01/1998 22 2500 M


3 Asha Tamil 12/12/1996 |20 2000 F

4 Zulu Computer | 05/09/2000 19 4500 M


Mala History 02/02/1998 25 3000 F

6 Maria Tamil 03/07/2000 22 3075 F

a) Create table STUDENT, with the following fields.


Field Type
No Integer
Name Char(20)
Dept Char(10)

Admn Date
Age Integer
Fee
Integer
Gender Char(2)
b) Insert data in the table STUDENT.
c) Alter the table STUDENT with the new attribute, father's name with the
d) Modify the attribute, gender with type of Char(7) in the table STUDENT.
type of Char(10).
e) Drop the attribute father's name from the table
f) Update the table STUDENT, and modify the data.
STUDENT
g) Use Order by to display data in ascending/ descending order
h) Use delete command to remove the last field from the student table.
i) Group the table STUDENT by dept.
j) Give the Output of following:
i. Select COUNT(distinct dept) from student.
ii. Select MAX(age) from the student where Gender="F°
ii. Select MIN(age) from the student where Gender-"M.
iv. Select AVG(fee) from the student where Admn=01/01/2000".
v. Select SUM(age) from the student where Admn>'05/09/2000.

SQL Commands:
mysq> create database lab;
Query Ok
mysql>use lab;
database changed
a) mysql> create table student(name char(20),dept char(10),
Admn date, age

integer,fee integer, gender integer);


Query Ok
b) mysql> insert into student
values(1, Rakesh', 'computer','2000/02/20',23,4000,m');
Query OPk
mysql> insert into student values(2, jeni','history','1998/01/10,22,2500,
'm');
Query Ok
mysql> insert into student values(3,'asha,'tamil','1996/12/12',20,2000,'f):
Query Ok
2000/09/05',19,4500,'m'"):
mysql> insert into student values(4, Zulu','computer',
Query Ok
'1998/02/02',25,3000, f);
mysql> insert into student values(5,'mala','history',
Query Ok
2000/07/03',22,3075,'f);
mysql> insert into student values(6,'maria','tamil',
Query Ok
fathername char(10);
c) mysql>alter table student add
Query Ok
d) mysql> alter table student modify gender char(7);
Query Ok
student drop column fathername;
e)mysql> alter table
Query Ok
)mysql> update student set dept="english' where name='asha';
Query Ok

admn;
g)mysql>select * from student order by
no nane dept adnn age fee gender
english} 1996-12-12 20 12000
asha 1998-01-10 22 I 2500 1m
jeni history 25 3000
mala history I 1998-02-02
Pakesh computer 2006-02-203 4000
3075
tanil7 2000-07-03 1
maria
2ulu conputer i 2560-09-05 4500
desc:
select *
from student order by name
mysq>
Iadmn age fee gender
no nane dept
computer 2000-09-05 19 4500
2ulu 23 4000
rakesh computer 2000-U2-20
2000-07-03 22 30?5
marla tani 1998-02-02 25 3000
mala ! history 22 2500 n
Jen 1998-01-10 2000 f
asha english 1996-12-12 20

h) mysql> delete from student where no=6;


Query Ok

mysql> select nanme,admn from student group by dept;


nane adnn

rakesh 2000-02-20
asha 1996-12-12
jeni 1998-81-10

)
1. mysql>select count(distinct dept) from student;

count<dist inct dept


3

mysql>select max(age) from student where gender=f;


nax(age)
25

iii. mysq select min(age),name from student where gender='m';


minKage)nane
19 rakesh

iv. mysql>select avg(fee) from student where admn='01/01/2000";


augCfee)
NULL

V. mysql>select sum(fee) from student where admn>'05/09/2000;


sunCfee
16000

Result
Thus the sQL commands are verified and executed successfully.
17. SQL COMMANDS FOR THE RELATIONS STATIONARY AND CONSUMER

AIM:

To write SQL commands for the following on the basis of given relations.

Table: Stationary
SID StationaryNane Company Price
DPO1 Dot Pen ABC 10
PLO2 Pencil XYZ
ERO5 Eraser XYZ
PLO1 Pencil CAM

GPO2 Gel Pen ABC 15

Table: Consumer

C_ID ConsumerName Address SID


01 Good Learner Delhi PLOO

06 Write Well Mumbai GP02

12 Topper Delhi DPO1

15 Write & Draw Delhi PLO2

16 Motivation Bangalore PLO1

a) To display the details of those consumers whose Address is Delhi.


b) To display the details of Stationary whose Price is in the range of 8 to 15.
c) To display the ConsumerName, Address from Table consumer, and Company and price
from table stationary ,with their corresponding matching S_ID.
d) To increase the Price of all stationary by 2.
e) Select Distinct Address from Consumer. GROUPBY
1) Select company MAX(price),MIN(price),cOUNT(*) from stationary
company.
Price from
g) Select Consumer. ConsumerName, Stationary. StationaryName Stationary.
,

stationary,Consumer where Consumer.S_ID -Stationary.S_ID


h) Select Stationary Name, Price*3 from Stationary

SQL Commands:
mysql>use lab;
database changed
mysql>create table stationary(s_id varchar(20),stationaryname char(20),company char(10),price
integer);
Query Ok
mysql>create table consumer(c_id integer,consumername char(20),address char(10),s_id
varchar(10);
Query Ok
mysql>insert into stationary values('DPO1',' DotPen',' ABC',10);
Query Ok
mysql>insert into stationary values('PL02',' Pencil',' XYZ',6);
Query Ok
mysql>insert into stationary values("ERO5','Eraser',' XYZ',7);
Query Ok
mysql>insert into stationary values(°PLO1','Pencil',' CAM',5);
Query Ok
mysql>insert into stationary values( GP02',' GelPen',' ABC',15):
Query Ok
mysq>insert into consumer values(01,' goodlearner',' delhi'", pl00");
Query Ok
mysql>insert into consumer values(06,'writewell',mumbai",'gp02");
Query Ok
mysql>insert into consumer values(12,'topper,' delhi','dp0l');
Query Ok
mysqinsert into consumer values(15,'write&draw',' delhi','pl02');
Query Ok
mysq>insert into consumer values(16,'motivation','bangalore','pl01):
Query Ok
. select *from consumer where address= delhi':
mysq>
consunername address sid
C_id
de lhi p100
goodlearner de lhi dpbl
topper
Write draw de lhi p102
and 15:
11.
mysqi>select
*
from stationary where price between 8
s-id stationarynane conpany price
dp01 dotpen abc
9p02 gelpen abc

where
111. mysql select consumername.address.company.price from consumer.stationary

consumer.s_id=stationary.s id;
Consunernane address conpany price
.---

topper del abc 10


Write&draw delhi Xy2
motivat ion bangalore Cam
writewe ll munbai i abc

V. mysql>update stationary set price=pricet2:


Query Ok
V. mysql>select distinct address from consumer;
address
de lhi
nunbai
bangalore
V1 mysql>select company.max(price),min(price).count(*) from stationary group by
company
conpany nax(price) min price) count (*)
abc
cam

xyz 9

vii. mysql>select consumer.consumername,stationary.stationaryname,stationary.price from


stationary.consumerwhereconsumer.s id=stationary.s_id;
T
Consunername stationaryname price
-+-- =-t
writewell gelpen 17
topper dotpen 12
write&draw penc i
notivation pencil

VI1. mysql>select stationaryname.price*3 from stationary;


stationaryn ane price*3
dotpen b
pencil 2
eraser
pencil
gelpen

Result:
Thus the SQL commands are verified and executed successfully.

18.SQL COMMANDS FOR THE RELATIONS DOCTOR AND SALARY


Aim:
To create the following relations DOCTOR and SALARY. On the basis of these two
tables, write SQL commands for (i) to (v) and show the outputs for the command (vi) and (vii).
Table: Doctor
ID NAME DEPT SEX EXPERIENCE
101 John ENT M 12
104 Smith ORTHO M
107 George Cardio M 10
114 Lara Skin
109 K George Medicine F
105 Johnson Ortho M 10
117 Lucy ENT F

111 Bill Medicine F 12


130 Morphy Ortho M 15

Table: Salary
1D BASIC ALLOWANCE CONSULTATION
101 12000 1000 300

104 23000 300 S00

107 32000 4000 500


114 12000 5200 100

109 42000 1700 200

105 18900 1690 300

130 21700 2600 300


i) Display name of all medicine specialists having more than 10 years of experience from
the table Doctor.
ii) Display the name ofthe Doctors who is having first letter as L'
ii) Display the average salary of all doctors working in ENT department using the tables
Doctor and Salary( salary-Basic + Allowance)
iv) Display the minimum alowance of female doctors.
v) Display the name of male doctors having highest consultation fee.
vi) Select count(*) from Doctor where sex='F';
vii) Select Name, Dept. Basic from Doctor,Salary where dept="ENT' and
Doctor.ID=Salary.ID;
sQL COMMANDS:
mysql> use lab;
Database changed
mysql> create table doctor (id integer,name char(20).dept char(20).sex char(10).experience
integer);
Query OK
mysql> create table salary(id integer,basie integer,allowance integer.consultation integer):
Query OK
mysql> insert into doctor values(101,john''ent',m',12);
Query OK
mysql> insert into doctor values( 104,smith'.'ortho','m.5);
Query OK
mysql> insert into doctor values(107.george'.eardio',m', 10):
Query OK
mysql> insert into doctor values(114.lara, skin'.T,3):
Query OK
George'.'Medicine.F.9)
mysql insert into doctor values(109.K
Query OK
mysql> insert into doctor values(105.Johnson' Ortho'.M.10):
Query OK
mysq> insert into doctor values(117.Lucy'. ENT,F.3)
Query OK
mysq> insert into doctor values(111.Bill.Medicine'.T.12):
Query OK
mysq> insert into doctor values(130.Morphy',Ortho'. M.15);
Query OK
mysql> insert into salary values(101,12000,1000,300):
Query OK
mysql> insert into salary values(104,23000,2300,500):
Query OK
mysql> insert into salary values(107,32000,4000,500):
Query OK
mysql> insert into salary values(1 14,12000,5200,100):
Query OK
mysql> insert into salary values(109,42000.1 700,200):
Query OK
mysql> insert into salary values(105,18900,1690,300);
Query OK
mysq>insert into salary values(130,21700,2600,300);
Query OK
and experience> 10;
i) mysql> select name from doctor where dept='medicine'
nane
Bill
like "1%;
ii) mysql> select id,name.experience from doctor where name

- -+---==-t-=-
id nane experience

114 lara
Lucy

ii) mysql> select avg(basic+allowance) from doctor,salary where dept=ent' and


doctor.id=salary.id;
avgCbas ic +allowance

13000.0800

iv) mysql> select min(allowance) from doctor,salary where sex=f' andd


loctor.id-salary.id;
minCallowance
1708

select max(consultation) from doctor,salary where sex=m' and


v) mysq>
doctor.id-salary.id;
max consultation

select count(*) from doctor where sex=T;


vi) mysql>
count (*)

select name,dept,basic from doctor,salary where dept=ent' and


vii) mysql>
doctor.id-salary.id:
name dept bas ic
John ent 12000
ult
Thus the SQL commands are verified and executed successfully.
19.SQL COMMANDS FOR THE RELATIONS FLIGHTS AND FARES
Aim
To create the following relations FLIGHTS and FARES. On the basis of these two tables,
write SQL commands for (i) to (v) and show the outputs for the command (vi).
TABLE:FLIGHT

FL_NO STARTINGFLIGHT ENDING NOFLIGHT NO STOPS


IC301 MUMBAI DELHI 8 0
C7991 BANGALORE DELHI
MC101 INDORE MUMBAI
IC302 DELHI MUMBA 8
AM812 KANPUR BANGLORE 3
IC899 MUMBAI KOCHI 4
AM501 DELHI TRIVENDRUM 1
MU499 MUMBAI MADRAS 3
IC701 DELHI AHMEDABAD 4

TABLE: FARE

FLNOO AIRLINES FARE TAX%


INDIAN AIRLINES 6500 10
IC701 5
MU499 SAHARA 9400
AM501 JET AIRWAYS 13450 8

IC899 INDIAN AIRLINES 8300 4


IC302 INDIAN AIRLINES 4300 10
IC799 INDIAN AIRLINES 10500 10
MC101 DECCAN AIRLINES 3500 4

"KANPUR" TO "BANGALORE" from the


i) Display FL_NO and NO_FLIGHTS from
table FLIGHTS.
Arrange the contents of the table FLIGHTS in the ascending order of FL NO.
ii)
be paid for the flights from DELHI to MUMBAI using the
ii) Display the FL_NO and fare to
FARE+FARE+TAX%/100.
tables FLIGHTS and FARES, where the fare to be paid
=

from the tables FARES.


iv) Display the minimum fare "Indian Airlines" offering
is
airlines.
v) To display the detail fares of Indian Where STARTING
Select FL_NO,NO_FLIGHTS,AIRLINES from FLIGHTS, FARES
=

vi)
DELHI" AND FLIGHTS.FL_NO = FARES.FL_NO

SQL COMMANDS:

mysq create database lab;


mysql> use lab;
mysql> create table flights(fl_no varchar(20),startingflight varchar(30).ending
varchar(30),no_flight int,no_stops int);
Query OK

mysql> INSERT INT0 FLIGHTS VALUES(TC301,MUMBAI,DELHIT,8,0);


Query OK
mysql> INSERT INTO FLIGHTS VALUES(C7991, BANGALORE,DELHI,2,1);
Query OK
mysql> INSERT INTO FLIGHTS VALUES('MC101',1NDORE,MUMBAI,3.0):
Query OK
mysql> INSERT INTO FLIGHTS VALUES(IC302. DELHI. MUMBAT,8, 0 );
Query OK
mysql> INSERT INTO FLIGHTS VALUES(AM812', KANPUR.BANGLORE.3, 1 );
Query OK
mysq> INSERT INTO FLIGHTS VALUES(IC899,MUMBAI KOCHI, 1
Query OK 1.4)
mysql> INSERT INTO FLIGHTS VALUESCAM501', DELHT,TRIVENDRUM. .
Query OK
>INSERT INTO FLIGHTS VALUES(MU499,:MUMBAI;MADRAS'.3.3 .
Query OK
mysq> INSERT INTO FLIGHTS VALUES(IC701' DELHT AHMEDABAD'4.0)
Query OK

mysql> CREATE TABLE FARES(FL NO VARCHAR(20).AIRLINES


VARCHAR(30).FARE INT,TAX INT);
Query OK
mysql> INSERT INTO FARES VALUE(IC701',INDIAN AIRLINES',6500,10);
Query OK
mysql> INSERT INTO FARES VALUE(MU499',
Query OKK SAHARA',9400,5);
mysql> INSERT INTO FARES VALUE(AM501',JET AIRWAYS'.13450,8);
Query OK
mysql> INSERT INTO FARES
VALUE(IC899, INDIAN AIRLINES',8300.4);
Query OK
mysql> INSERT INTO FARES
VALUEÇIC302',INDIAN AIRLINES,4300,10)
Query OK
mysql> INSERT INTO FARES VALUE(IC799',INDIAN
Query OKK AIRLINES',10500,10);
mysql> INSERT INTO FARES VALUE(MC101,DECCAN AIRLINES',3500,4);
Query OK
mysql> Select FL_NO, NO_FLIGHTS from FLIGHTS where
Startingflight="KANPUR" AND ENDING="BANGLORE";
FI N NO_FI TGHTS
AM812 3

11. mysql> select * from flights order by fl_no;

t--=---
fl no startingflight | ending no_flights | no stops
t -t-
AM501 DELHI TRIVENDRUM
AMB12 KANPUR BANGLORE
C799I BANGALORE DELHI 2 1
IC301 MUMBAI DE LHI 8
IC302 DELHI LMUMBAI 8
IC701 DE LHI TAHMEDABAD 4
IC899 MUMBAT T KOCHI
MC101 INDORE MUMBAI 3
MUA99 MUMBAI MADRAS 3 3
------
111 mysql> Select flights.fl_no, FARE+FARE+(TAX/100) from FLIGHTS, FARES where
Startinglight"DELHI" AND Ending-="MUMBAI;
fno FARE FARE (TAX/100)
+-
IC302 13000.10ee
IC302 18800.05e0
IC302 26900.08e0
IC302 1 16600.D4eo
IC302 8600.1000
IC302 21000.100e
IC302 7000.0400
mysql> Select min(FARE) fronm FARES Where AlRLINES="Indian Airlines"

nin(FARF)I

4303

m y s q > select * from fares where airlines="indian airlines":

FL_NO AIRLINES FARE TAX

IC701 INDIAN AIRLINES 6500


IC899 1NDLAN AIRLINES 8306 4
IC302 INDIAN AIRLINES 4300 10
IC799 INDIAN AIRLINES 10500 10

V1.
FLNO | NO_FLIGHTS I ATRLINES

IC701 1 4 INDIAN AIRLINES


AM501 1 1 1FT ATRUAYS
IC302 8 INDIAN AIRLINES
- - - - - - -

Result verified and executed successfully.


Thus the SQL commands are
RELATIONS ITEM AND TRADERS
20. SQL COMMANDS FOR THE

AIM:
To create two tables for item and traders and execute the given commands using SQL.

TABLE: 1TEM
Qty Price Company TCode
Code IName

DIGITAL PAD 121 120 11000 XENTIA TO1


1001
LEDSCREEN 40 70 38000 SANTORA TO2
1006
1004 CAR GPS SYSTEM 50 2150 GEOKNOW TO1

1003 DIGITAL CAMERA 12X 160 8000 DIGICLICK T02

1005 PEN DRIVE 32GB 600 200STOREHOME TO3


TABLE: TRADERS

TCode TName City


TO1 ELECTRONICS SALES MUMBAI
TO3 BUSY STORE CORP DELHI
TO2 DISPHOUSE INC CHENNAI

i) To display the details of all the items in ascending order of item names (i.e IName)
ii) To display item name and price of all those items, whose price is in the range of
10000 and 22000 (both values inclusive)
ii) To display the number ofitems, which are traded by each trader. The expected
output of this query should be
TO1
TO2
TO3
d i s p l a y the Price. item name (i.e IName) and quantity (i.e Qty) of those item
which have quantity more than 150.
v) To display the names of those traders, who are either from DELHI or from
MUMBAl.
CREATE TABLE ITEM(Code int . IName char(25).Qty int. Price int, Company
char(25).
TCode char(5):
INSERTINTO ITEM VALUES(1001,"DIGITAL PAD 121".120. 11000."XENTIA.
TO1 "):
INSERT INTO ITEM VALUES(1006,"LED SCREEN 40".70. 38000,"S.ANTORA".
T02"):
CREATE TABLETRADERS(TCode char(5). TName char(25), City char(20):
INSERT INTO TRADERS VALUES("TO1"."ELECTRONICS
SALES."MUMBAl"):
INSERTINTO TRADERSs VALUES("T03":"BUSY STORE CORp":"DELHT):
i) select * from ITEM order by IName;

Code IName Qty Price Company TCode


1004
CAR GPS SYSTEM 50 2150 GEOKNOpW TO1
1003 DIGITAL CAMERA 12X 160 8000 DIGICLICk T02
1001 DIGITAL PAD 121 120 I1000 XENTIA TOI
1006 LED SCREEN 70 38000 SANTORA TO2
1005 PEN DRIVE 32GB 600 1200 STORE HOME T03
S

elect IName , Price from ITEM where Pricebetween 10000 and 22000:
IName Price
DIGITAL PAD 121 11000

ii) select TCode, count(*)from ITEM group by TCode


Tcode Count()
TO1
TO2
TO3
iv) select Price,IName,Qtyfrom ITEM where Qty&gt:150:
Price IName Qty
8000 DIGITALCAMERA 12X 160
1200 PEN DRIVE 32GB 600

v)select TName from TRADERS where City in ("DELHI","MUMBAI"):


TName
ELECTRONICSSALES
BUSY STORE CORP

RESULT:
Thus the given program executed successfully.

21. INTEGRATE SQL WITH PYTHON BY IMPORTING THE MYSQL MODULE TO


CREATE DATABASE,TABLE AND INSERT VALUE IN THE TABLE

AIM:
To connect with database and create database and create table and insert value in the table.
ALGORITHM:
Step 1: Create the connectorobject sqltor for the class mysql connector.
Step 2: Create Connection Object myeon using the connector Object sqltor.
Step 3: Create Cursor object 'cursor' through myeon.
Step 4: Execute the query using cursor.execute()
Step 5: Close the connection object as mycon.close()
Coding:
import mysql.comnector as sqltor

con=sqltor.connect(host=localhost, user=root.password-admin')
if con.is_connected()==False:
print("error connecting to MySQL database")

cursor=con.cursor()

cursor.execute("create database lab3")

cursor.execute("use lab3")
cursor.execute("create table emp(name varchar(20))")

for i in range(5):
name=input("enter the name")

st="insert into emp(name) values('{}')".format(name)

cursor.execute(st)

con.commit()

con.close()

output
enter the name alma

enter the name jerry

enter the name riya

enter the name kala

enter the name Fatima

RESULT: for connecting with database.


Thus the program is executed successfully

MYSQL MODULE TO
PYTHON BY IMPORTING THE
22.INTEGRATE SQL WITH
TABLE
SELECT VALUE FROM THE

AIM:
record of the student.
To connect with database and select first three
ALGORITHM:
for the class mysql connector.
Step 1: Create the connector object sqltor
using the connector Object sqltor.
Step 2: Create Connection Object mycon
'cursor' through mycon.
Step 3: Create Cursor object
cursor.execute()
Step 4: Execute the query using
connection object as mycon.close(0
Step 5: Close the

CODING:
import mysql.comnector as sqltor
c o n = s q l t o r . c o n n e c t ( h o s t = l o c a l h o s t , u s e r = r o o t ' , p a s s w o r d = - a d m i n ' )

if con.is_connected()=False:
print(error connecting to MySQL database")
cursor=con.cursor())
cursor.execute("create database lab5")
cursor.execute("use lab5")
varchar(20))")
cursor.execute("create table emp(name
for i in range(3):
name-input("enter the name")
st-"insert into emp(name) values('}')".format(name)
cursor.execute(st)
con.commit()
cursor.execute("select * from emp")

data-cursor.fetchmany(3)
count=cursor.rowcount
for row in data:
print(row)
con.close()
OUTPUT:
enter the name AlLMA
enter the name ANNIE
enter the name RIMO
ALMA')
(ANNIE,)
RIMO'.)
RESULT:
Thus the program is
executed
successfully for connecting with database.
23. INTEGRATE sQL WITH PYTHON BY
DELETE VALUE FROM THE TABLE IMPORTING THE MYSsQL MODULE TO
AIM:
To connect with database and
delete the record from the table.
ALGORITHM:
Step 1: Create the connector object sqltor for the class
Step 2: Create Connection Object mycon mysqi connector.
Step 3: Create Cursor object 'cursor' using the connector
Object sqltor.
Step 4: Execute the query using through mycon.
cursor.execute()
Step 5: Close the connection object
CODINGS:
as
mycon.close()
import mysql.connector as sqltor

con=sqltor.connect(host="localhost'
if con.is_connected()=False:
,user=root.password='admin')
print("error connecting to MysQL database")
cursor=con.cursor()
cursor.execute("ereate database lab5")
cursor.execute("use lab5")
cursor.execute("create table emp(name varchar(20))")
for i in range(3):
name=input("enter the name")
st="insert into emp(name) values(}')".format(name)
cursor.execute(st))
con.commit()
cursor.execute("delete from emp where name='alma ")

con.commit()
con.close0
QUTPUT:
cnter the name alma
enter the name annie
enter the name rimo
RESULT:
Thus the program is executed successfully for connecting with database.

24. INTEGRATE SQL WITH PYTHON BY IMPORTING THE MYSQL MODULE TO


UPDATE VALUE FROM THE TABLE
AIM:
To connect with database and update the record of the student.
ALGORTHM:
Step 1: Create the connector object sqltor for the class mysql connector.
Step 2: Create Connection Object mycon using the connector Object sqltor.
Step 3: Create Cursor object 'cursor' through mycon.
Step 4: Execute the query using cursor.execute()
Step 5: Close the connection object as mycon.close()
CODING:
import mysql.connector as sqltor

con-sqltor.connect(host="localhost',user="root,password='admin',database='student')
cursor-con.cursor()
cursor.execute("update student set mark1=90 where name=raja' ")

con.commit()

print(cursor.rowcount,"record(s) affected")
con.close0
OUTPUT:

I record(s) affected

RESULT:
Thus the program is executed successfully for connecting with database.

You might also like