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

Interface Python With MYSQL

This document discusses how to interface Python with MySQL. It provides steps to install the MySQL connector module, connect to a MySQL database from Python, execute SQL statements using a cursor object, and close connections. Key points covered include importing the MySQL connector API, using the connect() method to establish a database connection, the cursor() method to execute queries, and commit(), rollback(), autocommit to manage transactions. Methods for inserting, selecting, updating, and deleting data from MySQL tables in Python programs are demonstrated.

Uploaded by

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

Interface Python With MYSQL

This document discusses how to interface Python with MySQL. It provides steps to install the MySQL connector module, connect to a MySQL database from Python, execute SQL statements using a cursor object, and close connections. Key points covered include importing the MySQL connector API, using the connect() method to establish a database connection, the cursor() method to execute queries, and commit(), rollback(), autocommit to manage transactions. Methods for inserting, selecting, updating, and deleting data from MySQL tables in Python programs are demonstrated.

Uploaded by

rupeshkr.3905
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

INTERFACE PYTHON WITH MYSQL

• The python programming language has powerful features for database programming.
• Python supports various databases like MySQl,Oracle,Sybase,Postgre(SQL),etc.
• Python also supports DDL and DML statements.
• Python DB-API(database application programming interface) is a widely used module that
provides a database application programming interface.
• Python database API supports a wide range of database servers:-
✓ MYSQL
✓ PostgreSQL
✓ Oracle
✓ Sybase
✓ Microsoft SQL Server 2000

PYTHON MYSQL CONNECTIVITY:-


• Usually the data inputted by the user and the output being displayed is not
stored somewhere because all program takes place inside the RAM which is
a temporary memory.
• And as soon as we close the form ,its contents gets erased.
• The contents cant be retrieved since they are not saved on a hard disk.
• This limitation can be overcome by sending the output generated and saving
the input fetched from the user in a database created at the back end of the
application.The input is fetched from the user using python interface.
This is termed as the front end interface of the application.
• Now getting the data from the front end is saved permanently on the
database.This database is called back end database.
• Python supports SQL cursor
• Python supports relational database system.

INSTALLING MYSQL CONNECTOR

To establish the connection between python and MYSQL we require python


database interfaces and APIs.
The DB-API provides a minimal standard for working with databases using
python structures and syntax wherever possible.
To accomplish the whole task:-
1. Install mysqlconnector(one time activity)
2. Import the API module
import mysql.connector
3. Create a connection with the database
connect(host,user,password,databasename)
4. Issue SQL statements
cursor()
ddl/dml
5. Close the connections
close()

First of all we have to install the mysql-connector using pip


command in the python path.

To check whether mysql connector is installed or not,type

If no error comes,this shows that driver has successfully installed.


connect(host=”localhost”,user=”root”,passwd=””)

ESTABLISHING CONNECTION:-

The next step is to make the connection with the database we wish to use.

All python DB-API modules implements a function:

Module_name.connect()

The connect() statement creates a connection to the MYSQL server and returns a mysql connection
object.

The second statement creates a connection object ‘mydb’ through localhost for mysql with
username as ‘root’ and password as blank in the above case.

Arguments required for connect() method:-

a. Username:-this is the username that we use to work with MySQL server,The default is
“root”.
b. Password-If we are using root then we don’t need to specify password.
c. Hostname:-This is the server name or IP address on which MYSQL is running.
d. Database name
All are optional keyword arguments for connect() method.

CREATING CURSOR OBJECT:-

The next step after creating connection using connect() is to create cursor object.The cursor object
let us execute all the queries we need.It gives us the ability to have multiple separate working
environments through the same connection to the database.

• For this we need to create object of class cursor


• Cursors are created by the connection.cursor() method.
To accomplish the whole task:
1. Create connection using mysql.connector.connect()
2. Create cursor object with the help of connection object returned by connect() in above
step to execute query.
3. Type cursor.execute() to execute sql queries from python
4. Close the cursor object using cursor.close() and connection object using
connection.close() when all work is done.

CREATING A DATABASE:-
ALL STEPS IN A GLANCE:-

1. import mysql.connector
In case the connection has failed or any other database error occurred while working on it
write:-
from mysql.connector import Error
2. mysql.connector.connect(host,database,user,passwd)
3. conn.is_connected()
It is one of the function of MYSQL coonection class through which we can check out python
application is connected to MySQL application or not.
4. cursor():-This method returns a cursor object which is used to execute SQL statements.
5. cursor().execute()
After writing the commit() the changes will be seen in the database

Methods to manage MySQL database transactions in python:

a. commit():-this method sends a commit statement to the MySQL server committing the
current transaction.
Syntax:-
connection.commit()
b. rollback():-It reverts the changes made by the current transaction.
Syntax:-
connection.rollback()
c. autocommit:-It is used to enable or disable the auto commit feature of MYSQL.
Connection.autocommit value can be assigned as True or False.By default it is False.

Inserting multiple records

Displaying the table:-


In the above program we have used select() along with fetchall() function.

As we are performing the READ operation on the database to fetch some information using select
command

So select can be used with any of the functions mentioned below:

1. fetchone():-to fetch single record.It fetches next row of a result set.


2. fetchall():-to fetch multiple values from a database table.It fetches all rows in a result set.If
some rows have already been extracted from the result set,then it retrieves the remaining
rows from the result set.
3. rowcount:-It returns the number of rows that were affected by an execute().
Displaying the record for amount>50000(no need to write commit)
Now deleting the record with stock id=1

Displaying the table

Updating value and Counting the count of rows using the attribute rowcount:-
CLOSING CURSOR AND CONNECTION:-

cursor.close()

OR

mycursor.close()

OR

conn.close()

You might also like