Python - Mysql Database Access: Gadfly MSQL Mysql Postgresql Microsoft SQL Server 2000 Informix Interbase Oracle Sybase
Python - Mysql Database Access: Gadfly MSQL Mysql Postgresql Microsoft SQL Server 2000 Informix Interbase Oracle Sybase
Python - Mysql Database Access: Gadfly MSQL Mysql Postgresql Microsoft SQL Server 2000 Informix Interbase Oracle Sybase
The Python standard for database interfaces is the Python DB-API. Most Python
database interfaces adhere to this standard.
You can choose the right database for your application. Python Database API supports
a wide range of database servers such as −
GadFly
mSQL
MySQL
PostgreSQL
Microsoft SQL Server 2000
Informix
Interbase
Oracle
Sybase
Here is the list of available Python database interfaces: Python Database Interfaces
and APIs. You must download a separate DB API module for each database you need
to access. For example, if you need to access an Oracle database as well as a MySQL
database, you must download both the Oracle and the MySQL database modules.
The DB API provides a minimal standard for working with databases using Python
structures and syntax wherever possible. This API includes the following −
import MySQLdb
If it produces the following result, then it means MySQLdb module is not installed −
Traceback (most recent call last):
File "test.py", line 3, in <module>
import MySQLdb
ImportError: No module named MySQLdb
To install MySQLdb module, use the following command −
For Ubuntu, use the following command -
$ sudo apt-get install python-pip python-dev libmysqlclient-dev
For Fedora, use the following command -
$ sudo dnf install python python-devel mysql-devel redhat-rpm-
config gcc
For Python command prompt, use the following command -
pip install MySQL-python
Note − Make sure you have root privilege to install above module.
Database Connection
Before connecting to a MySQL database, make sure of the followings −
You have created a database TESTDB.
You have created a table EMPLOYEE in TESTDB.
This table has fields FIRST_NAME, LAST_NAME, AGE, SEX and INCOME.
User ID "testuser" and password "test123" are set to access TESTDB.
Python module MySQLdb is installed properly on your machine.
You have gone through MySQL tutorial to understand MySQL Basics.
Example
Following is the example of connecting with MySQL database "TESTDB"
#!/usr/bin/python
import MySQLdb
While running this script, it is producing the following result in my Linux machine.
Database version : 5.0.45
If a connection is established with the datasource, then a Connection Object is
returned and saved into db for further use, otherwise db is set to None. Next, db object
is used to create a cursor object, which in turn is used to execute SQL queries. Finally,
before coming out, it ensures that database connection is closed and resources are
released.
Creating Database Table
Once a database connection is established, we are ready to create tables or records
into the database tables using execute method of the created cursor.
Example
Let us create Database table EMPLOYEE −
#!/usr/bin/python
import MySQLdb
cursor.execute(sql)
import MySQLdb
import MySQLdb
import MySQLdb
import MySQLdb
1
Warning
Used for non-fatal issues. Must subclass StandardError.
2
Error
Base class for errors. Must subclass StandardError.
3
InterfaceError
Used for errors in the database module, not the database itself. Must subclass Error.
4
DatabaseError
Used for errors in the database. Must subclass Error.
5
DataError
Subclass of DatabaseError that refers to errors in the data.
6
OperationalError
Subclass of DatabaseError that refers to errors such as the loss of a connection to
the database. These errors are generally outside of the control of the Python scripter.
7
IntegrityError
Subclass of DatabaseError for situations that would damage the relational integrity,
such as uniqueness constraints or foreign keys.
8
InternalError
Subclass of DatabaseError that refers to errors internal to the database module, such
as a cursor no longer being active.
9
ProgrammingError
Subclass of DatabaseError that refers to errors such as a bad table name and other
things that can safely be blamed on you.
10
NotSupportedError
Subclass of DatabaseError that refers to trying to call unsupported functionality.