0% found this document useful (0 votes)
25 views10 pages

DAY2 Databaseconnectivity

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)
25 views10 pages

DAY2 Databaseconnectivity

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

Unit III Database Management

Topic 1/5: Interface of Python with an SQL database

Learning Objectives:
 About database connectivity using MySql at backend and Python at FrondEnd
 Student will learn functions used for performing database connectivity
 The use of DB connectivity will be further used in Project Implementation.

For interfacing with Python it is mandatory to install mysql connector .It could be installed from
[Link]

# database connectivity Prog1

import [Link] as con


mycon=[Link](host="localhost",user="root",passwd="root",database="pari_db")
if mycon.is_connected():
print("connected")
else:
print("not connected")
cursor=[Link]()
[Link]("select * from emp")
data=[Link]()
cnt=[Link]
for row in data:
print(row)
[Link]()

Step1. Import mysql connector and make a reference

Step 2. Create a connection .It requires four parameters.


A. Host: host="localhost" B. User : user="root" C. Password: passwd="root"
D. Name of Database where table exists: database="pari_db"

Step 3: Check whether connection was established STEP 4: Create cursor using cursor()
if mycon.is_connected(): // Use is_connected method
method
print("connected")
else: cursor=[Link]()
print("not connected") cursor is a pool of values where the
records fetched from database is
stored. It is created temporarily.

Database connection can be achieved using these four step process.


Unit III Database Management
Topic 2/5: Connecting SQL with Python

Learning Objectives:
1. This topic will allow the students to learn functions/methods used in connecting database

Function Syntax Use


connect() [Link]() Connect method is invoked by passing host,
username, password, database_name as
arguments.
Is_connected() con.is_connected() Used to check whether the connection established
with frontend application

cursor() X=[Link]() This method is used to create cursor(a temporary


buffer in memory)

execute() [Link](“SQL Query”) Execute() method will take SQL query as an


argument

executemany() [Link]() Accepts two arguments query and multiple values


in form of List. Used with insert query.

commit() [Link]() Commit method is used to save the transaction in


database.
close() [Link]() Close() method is used to close connection

rowcount() [Link]() Used the count the total no of rows populated in


the cursor.
fetchone() [Link]() Used to retrieve single record from cursor

fetchall() [Link]() Used to retrieve all the records from cursor


Unit III Database Management
Topic 3/5: Creating Database connectivity Applications
Learning Objectives:
1. Having learned about database connectivity student must be able to perform all the
operations on the databases from frontend i.e PTYHON
2. This topic will help the student to implement queries from frontend application.

#PROGRAM 2 DISPLAYING DATABASES

import [Link] as con

mycon=[Link](host="localhost",user="root",passwd="root",database="pari_db")

if mycon.is_connected():

print("connected")

else:

print("not connected")

cursor=[Link]()

[Link]("SHOW DATABASES") # displaying databases

data=[Link]()

#printing all the values as tuple

print(data)

#counting total values in cursor created

cnt=[Link]

print(cnt)

for row in data:

print(row)

[Link]()
#PROGRAM 3 CREATING DATABASES

import [Link] as con

mycon=[Link](host="localhost",user="root",passwd="root",database="pari_db")

if mycon.is_connected():

print("connected")

else:

print("not connected")

cursor=[Link]()

[Link]("CREATE DATABASE TEST") # CREATING DATABASE

data=[Link]()

#printing all the values as tuple

print(data)

#counting total values in cursor created

cnt=[Link]

print(cnt)

for row in data:

print(row)

[Link]()
Unit III Database Management
Topic 4/5: Performing Insert, Update, Delete queries

Learning Objectives:
1. Performing DML queries.

#PROGRAM 7 INSERTING SINGLE RECORD

import [Link] as con


mycon=[Link](host="localhost",user="root",passwd="root",database="pari_db")
if mycon.is_connected():
print("connected")
else:
print("not connected")
cursor=[Link]()
QRSTMT="INSERT INTO DEPT1(DEPT_ID,DNAME,DLOC) VALUES (%s,%s,%s)"
VALUES=(2,"SALES","SHILLONG")
[Link](QRSTMT,VALUES) # execute method is used to insert
[Link]()
print("VALUES INSERTED")
[Link]("SELECT * FROM DEPT1")
data=[Link]()
#printing all the values as tuple
print(data)
#counting total values in cursor created
cnt=[Link]
print(cnt)
for row in data:
print(row)
#PROGRAM 6 INSERTING MULTIPLE RECORD

import [Link] as con

mycon=[Link](host="localhost",user="root",passwd="root",database="pari_db")

if mycon.is_connected():

print("connected")

else:

print("not connected")

cursor=[Link]()

QRSTMT="INSERT INTO DEPT(DEPT_ID,DNAME,DLOC) VALUES (%s,%s,%s)"

VALUES=[(1,"COMPUTER","SHILLONG"),(2,"SALES","LUCKNOW")]

[Link](QRSTMT,VALUES) # executemany method is used to insert

[Link]()

print("VALUES INSERTED")

[Link]("SELECT * FROM DEPT")

data=[Link]()

#printing all the values as tuple

print(data)

#counting total values in cursor created

cnt=[Link]

print(cnt)

for row in data:

print(row)
#PROGRAM 8 INSERTING multiple RECORD using loop

import [Link] as con


mycon=[Link](host="localhost",user="root",passwd="root",database="pari_db")
if mycon.is_connected():
print("connected")
else:
print("not connected")
cursor=[Link]()
x=int(input("enter the no records"))
for i in range(x):
val1=int(input("enter dept id"))
val2=input("enter dept name")
val3=input("enter dept loc")
QRSTMT="INSERT INTO DEPT1(DEPT_ID,DNAME,DLOC) VALUES (%s,%s,%s)"
VALUES=(val1,val2,val3)
[Link](QRSTMT,VALUES) # execute method is used to insert
[Link]()

print("VALUES INSERTED")
[Link]("SELECT * FROM DEPT1")
data=[Link]()
#printing all the values as tuple
print(data)
#counting total values in cursor created
cnt=[Link]
print(cnt)
for row in data:
print(row)
# updating record selective

import [Link] as con


mycon=[Link](host="localhost",user="root",passwd="root",database="pari_db")
if mycon.is_connected():
print("connected")
else:
print("not connected")
cursor=[Link]()
stmt1="UPDATE DEPT1 SET DLOC="
stmt2="WHERE DEPT_ID="
x=input("enter the location name")
y=input("enter dept id")
stmt=stmt1 +'"'+str(x)+ '"'+stmt2+str(y)
print(stmt)
[Link](stmt)
[Link]()

print("VALUES DELETED")
[Link]("SELECT * FROM DEPT1")
data=[Link]()
#printing all the values as tuple
print(data)
#counting total values in cursor created
cnt=[Link]
print(cnt)
for row in data:
print(row)
#PROGRAM 9 DELETE selective record

import [Link] as con


mycon=[Link](host="localhost",user="root",passwd="root",database="pari_db")
if mycon.is_connected():
print("connected")
else:
print("not connected")
cursor=[Link]()
stmt1="DELETE FROM DEPT1 WHERE DEPT_ID="
x=int(input("enter the dept id to be deleted"))
stmt=stmt1+str(x)
[Link](stmt)
[Link]()

print("VALUES DELETED")
[Link]("SELECT * FROM DEPT1")
data=[Link]()
#printing all the values as tuple
print(data)
#counting total values in cursor created
cnt=[Link]
print(cnt)
for row in data:
print(row)
Unit III Database Management
Topic 5/5: Display data by using fetchone (), fetchall(), rowcount()

Learning Objectives:
1. Fetching records from database

#retrieving records from database

# fetching records from database

import [Link] as con


mycon=[Link](host="localhost",user="root",passwd="root",database="pari_db")
if mycon.is_connected():
print("connected")
else:
print("not connected")
cursor=[Link]()
[Link]("select * from emp")
data=[Link]()
cnt=[Link]
for row in data:
print(row)

You might also like