Python MySQL Connectivity Notes Class 12

Python MySQL Connectivity Notes Class 12

 

Interface Python with SQL database

Contents:

♦  Connecting SQL with Python

♦  Creating database connectivity application

♦  Performing insert, delete, update, queries

♦  Display data by using fetchone(), fetchall(),fetchmany(), rowcount()

 

Database connectivity

Database connectivity refers to connection and communication between an application and a database system.

The term “front-end” refers to the user interface, while “back-end” means the server application and database that work behind the scenes to deliver information to the user.

mysql.connector-Library or package to connect from python to MySQL.

Before we connect the program with mysql, we need to install connectivity package named mysql-connector-python.

Command to install connectivity package:

pip install mysql-connector-python

 

Command to import connector:-

Import mysql.connector

 

Steps for python MySQL connectivity

Step 1: Install Python

Step 2: Install MySQL

Step 3: Open Command prompt & Switch on internet connection

Step 4: Type pip install mysql-connector-python and execute

Step 5: Open python IDLE

Step 6: Import mysql.connector

 

Steps for creating database connectivity applications

Step 1: Start Python: Start python editor to create your python script

Step 2: mysql.connector package

Step 3: Establishing connection to MySQL DATABASE

We need to establish a connection to a mysql database using connect() function of mysql.connector package.

The connect statement creates a connection to the mysql server and returns a MySQL connection object.

Syntax:

<Connection object>=mysql.connectorconnect (host=<hostname>, user=<username>, passwd <password>, database=<dbname>)

 

import mysql.connector

con=mysql.connector.connect(host=”localhost”, user=”root”,  passwd=” “)

 

Creating a cursor Object:

It is a useful control structure of database connectivity. It will let us execute all the queries we need. Cursor stores all the data as a temporary container of returned data and allows traversal so that we can fetch data one row at a time from cursor. Cursors are created by the connection.cursor() method.

 

Syntax:

<cursor object>=<connectionobject> .cursor()

Eg: Cursor=con.cursor()

 

Execute SQL query:

We can execute SQL query using execute() function

Syntax:

<cursor object>.execute(SQL QUERY)

Eg: cursor.execute(“select* from data”)

 

The above code will execute the sql query and store the retrieved records (resultset) in the cursor object(cursor).

Result set refers to a logical set of records that are fetched from the database by executing an sql query and made available in the program.

 

Extract data from Result set:

The records retrieved from the database using SQL select query has to be extracted as record from the result set. We can extract data from the result set using the following fetch() function.

fetchall()

fetchone()

fetchmany()

 

Ways to retrieve data

  • fetchall()-Fetches all (remaining) rows of a query result. returning them as a sequence of sequences (e.g. a list of tuples).
  • fetchone()-Fetches the next row of a query result set, returning a single sequence or None when no more data is available
  • fetchmany (size)-Fetches the next set of rows of a query result, returning a sequence of sequences. It will return number of rows that matches to the size argument.

 

To create database  school using python interface

import mysql.connector

mydb=mysql.connector.connect(host=”localhost”.user=”root”.passwd=”system”)

mycursor=mydb.cursor()

mycursor.execute(“CREATE DATABASE SCHOOL”)

 

Show database

import mysql.connector

mydb=mysql.connector.connect(host=”localhost” user=”root”, passwd=”system”)

mycursor=mydb.cursor()

mycursor.execute(“SHOW DATABASES”)

for x in mycursor:

print (x)

 

To create a table in mysql using python interface

import mysql.connector

mydb=mysql.connector.connect(host=”localhost”.user=”root”.passwd=”system”,

database=”student”)

mycursor=mydb.cursor()

mycursor.execute(“CREATE TABLE FEES (ROLLNO  INT,  NAME VARCHAR(20),  AMOUNT INT)”)

 

 

To show the tables in mysql using python interface

import mysql.connector

mydb=mysql.connector.connect(host=”localhost”,user=”root”, passwd=”system”, database=”student”)

mycursor.execute (“Show tables”)

for x in mycursor:

print (x)

 

To describe table structure  using python  interface

import mysql.connector

mydb=mysql.connector.connect(host=”localhost”,user=”root”, passwd=”system”, database=”student”)

mycursor.execute (“DESC STUDENT”)

for x in mycursor:

print (x)

 

To execute select query using python interface

import mysql.connector

mydb=mysql.connector.connect(host=”localhost”,user=”root”, passwd=”system”, database=”student”)

c= mydb.cursor()

c.execute (“select* from student”)

r=c.fetchone()

while r is none :

print (r)

r=c.fetchone()

 

Leave a Reply

Your email address will not be published. Required fields are marked *