Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
71 views

Interface Python With Mysql Notes

Uploaded by

notneedtome
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views

Interface Python With Mysql Notes

Uploaded by

notneedtome
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

MDKSH H .

INTERFACE PYTHON WITH MYSQL


Interface Pyhton with Mysql using python code o connect to any sql database,such as mysql.,
SqlServer, Oracle etc and perform operations on data stored in data base tables.
Patabase Connectivity:- refers to connection andcommunication between an application and a
database-that work behind the scenes to deliver intormation to the user.)
Term Front end -user interface(python)
Term Back end - Server Application(.Net, PHP,Apache, Python)
Steps to install python mysqlconnectivity:
Step1: Install python Step2: Install Mysql
Step3:Open command prompt &switch an internet connection.
Step4: Type pip install mysq connector-python Step5: Open Python IDLE
Step6: import mysql Connector
Steps to Creating database connectivity-applications:5)
Step1: Open Python- python editor to create python program in script mode.
Step2: import the, Package- import mysql.Connector or import mysql.connector as mys
Step3: Open a Connection todatabase
mycon=mysql.connector.connect(host-"localhost', user='root",paswd="vbc",database="school")
n i s conhected(): function is used to checking connection establishment. ctio' p
Step4: Create a Cursor mycursor: mycon.cursor) Control structure ,set of records retrieved
from the database as per query,Aut set
Step5: Execute query mycursor.execute("query"),(used toexecute sql queryland store the retrievec
regords(result set) in the cursor object.
t Result Setrefers to alogical set of record that are fetched from the database by executing an sql
query andnade available in the program) b.
Step6: Extract data from result set- using fetch functËon
(fetchone()- return one(first) record from the result set) it return return type as Tuple otherwise None
fetchmany() - return number of specified record from the result set! it return return type as List of
Tuple otherwiseempty list>
(fetchall()- return all the record from the result set,jt return return type is List of Tuple otherwise
empty list)
rowcount- attribute to count the number of records
Step7: Clean up the environment-to closethe connection establishment, mycon.close(0
Example:
Table Name: emp Databasce Name=employee_details # already emp table exists and it has 4
records.
empno name dept salary
1 arun IT 65000
2 kavitha CSC 60000
3 sanjana 75000
Manager
4 vishal Admin 35000

import mysql.connector

mycon.mysql.connector.connect(host="localhost",user="root",passwd="vbc",database="ymployee
details")
mycursor=mycon.cursor()
mycursor.erexeute("select *
row-mycursor.fetchmany(2)from emp")
fori in row:
print(;)
mycon.close()

Output:
empno name
1
arun
dept salary
2 IT
65000
kavitha CSC 60000
Note:- After executing the insert,
changes to the update and delete query must using
databse. commit () used to save the
Parameterized Queries: example: mycursor.commit()
Sometimes we need to run queries which are need on
outside. (or) some parameters or values that
We can pass values to provided from
query to perform dynamic search.
Various methods:
1.

exarnple:-Concatenating (+)variable with query:


empl*ofromyee_no=i
query="select nt(input("enter
emp where
the empno :)
Output:- enter the
1
empno:1
arun
empno"tstr(employee_no)
String tempiate witn%s 65000
then pass Formatting(oiG siyie): use 6s in
the values for that place. piace of vaiues to substite and
example:
employee_no=int(input("enter the empno :))
emplquery="select
oyee_Salary<i*nfrom
t(inputemp("entwhere
er theempno=%s
salary :))
and salary>=%s
Output:
enter. the empno:3
cnter the salary:70000
"%(employee_no,employee_salary)
3
sanjana Manager 75000
3, String template with {} and format
values inside () we can optionally give function(new style): In the place of %s use ) to pass
example:-(O},{1),(2}... or) (empno).{ 0,1,2....values.
name), {dept},{salary,.
example:

cmployee_no=int(input("enter the empno :))the salary :))


Guery="selectemployee_Salary=int(input("enter
from emp where
empno=(empno) and
".format(empno=employee_no,salary-employee_salary)
Output:
entor the empno:3
cnter the salary:70000
salary>={salary}

3
sanjana Manager 75000

You might also like