Python & MySQL
Python & MySQL
1
MySQL-Connector-Python module in Python
MySQL is a Relational Database Management System (RDBMS) whereas the structured Query
Language (SQL) is the language used for handling the RDBMS using commands i.e Creating,
Inserting, Updating and Deleting the data from the databases. SQL commands are case
insensitive i.e CREATE and create signify the same command.
What is MYSQL Connector/Python?
MySQL Connector/Python enables Python programs to access MySQL databases
Installing module
This module does not come built-in with Python. To install it type the below command
in the command prompt.
After installing the MySQL Python connector, we need to test it to make sure that it is
working correctly and we are able to connect to the MySQL database server without any
issues.
To verify the installation, we can use the following steps:
1. Open Python command line
2. Type the following code
>>>import mysql.connector
>>> mysql.connector.connect(host="localhost",user="root",passwd="amit“ , database=“abc”)
Output:
<mysql.connector.connection.MySQLConnection object at 0x02DD6CB0>
3
Connect MySQL database using MySQL-Connector Python
<connection-Object>=mysql.connector.connect(host=“host-name”, user=“user-name”,
passwd=“password” , database=“database-name”)
A database Connection object controls the connection to the database .It represent a unique
session with a database connected from within a script/program.
If db.is_connected( ):
print(“ Successfully connected with mysql database”)
db.close()
5
Python MySQL – Create cursor Instance
6
Python MySQL – Execute SQL Query
7
Python MySQL – Create Database
The above program illustrates the creation of MySQL database abc in which host-
name is localhost, the username is root and password is amit.
8
Python Mysql Connector Module Methods
1. connect() :
This Function is used for establishing connection with the MySQL server. The following are the
arguments that are used to initiate a connection.
• user: User name associated with the MySQL server used to authenticate the connection
• password: Password associated with the user name for authentication
• database: Database in the MySQL for creating the Table .
2. cursor()
Cursor is the work space created in the system memory when the SQL command is executed .
This memory is temporary and the cursor connection is bounded for the entire session/
lifetime and the commands are executed
3. execute()
The execute function takes a SQL query as an argument and executes . A query is any SQL
command which is used to create, insert, retrieve, update, delete etc.
9
Python: MySQL Create Table
db.close()
10
Python MySQL – Insert into Table
db.commit( )
db.close()
11
Python MySQL – Update Table
The update is used to change the existing values in a database. By using update a
specific value can be corrected or updated. It only affects the data and not the
structure of the table.
db.commit( )
db.close()
db.commit( )
db.close()
13
Python Program to fetch record(s) from a Table
Select command in SQL is used to fetch record from any existing table(s). There are three types of
records this command can produce
1. Single word
2. Single record
3. Multiple record
fetchone() method is used with database cursor() to pick one row from the cursor and
return it as a tuple().
Syntax of fetchone() method
result = cursor.fetchone()
db.close()
16
Python MySQL – Select Query (fetchall method)
rec= cur.fetchall( )
for i in rec:
print(i)
db.close()
17