mysql_database_access

= Python - MySQL Database Access =

  



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: Here is the list of available Python databases interfaces: code  Python Database Interfaces and APIs code || 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: > > > > We would learn all the concepts using MySQL so let's talk about MySQLdb module only. = What is MySQLdb? = MySQLdb is an interface for connecting to a MySQL database server from Python. It implements the Python Database API v2.0, and is built on top of the MySQL C API.
 * GadFly
 * mSQL
 * MySQL
 * PostgreSQL
 * <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: left;">Microsoft SQL Server 2000
 * <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: left;">Informix
 * <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: left;">Interbase
 * <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: left;">Oracle
 * <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: left;">Sybase
 * <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: left;"> Importing the api module.
 * <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: left;"> Acquiring a connection with the database.
 * <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: left;"> Issuing SQL statements and stored procedures.
 * <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: left;"> Closing the connection

How do I install the MySQLdb?
<span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">Before proceeding you make sure you have MySQLdb installed on your machine. Just type the following in your Python script and execute it: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">#!/usr/bin/python

import MySQLdb code || <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">If it produces following result then it means MySQLdb module is not installed: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">Traceback (most recent call last): File "test.py", line 3, in    import MySQLdb ImportError: No module named MySQLdb code || <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">To install MySQLdb module, download it from MySQLdb Download page and proceed as follows: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">$ gunzip MySQL-python-1.2.2.tar.gz $ tar -xvf MySQL-python-1.2.2.tar $ cd MySQL-python-1.2.2 $ python setup.py build $ python setup.py install code || <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">**Note:** Make sure you have root privilege to install above module. = Database Connection: = <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">Before connecting to a MySQL database make sure followings: > > > > > >
 * <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: left;"> You have created a database TESTDB.
 * <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: left;"> You have created a table EMPLOYEE in TESTDB.
 * <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: left;"> This table is having fields FIRST_NAME, LAST_NAME, AGE, SEX and INCOME.
 * <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: left;"> User ID "testuser" and password "test123" are set to access TESTDB
 * <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: left;"> Python module MySQLdb is installed properly on your machine.
 * <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: left;"> You have gone through MySQL tutorial to understand [|MySQL Basics.]

Example:
<span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">Following is the example of connecting with MySQL database "TESTDB" code <span style="font-family: 'Courier New',monospace; font-size: 12px;">#!/usr/bin/python

import MySQLdb

db = MySQLdb.connect("localhost","testuser","test123","TESTDB" )
 * 1) Open database connection

cursor = db.cursor
 * 1) prepare a cursor object using //cursor// method

cursor.execute("SELECT VERSION")
 * 1) execute SQL query using //execute// method.

data = cursor.fetchone
 * 1) Fetch a single row using //fetchone// method.

print "Database version : %s " % data

db.close code || <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">While running this script, its producing following result at my Linux machine. code <span style="font-family: 'Courier New',monospace; font-size: 12px;">Database version : 5.0.45 code || <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">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. <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">Finally before coming out it ensures that database connection is closed and resources are released. = Creating Database Table: = <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">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.
 * 1) disconnect from server

Example:
<span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">First let's create Database table EMPLOYEE: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">#!/usr/bin/python

import MySQLdb

db = MySQLdb.connect("localhost","testuser","test123","TESTDB" )
 * 1) Open database connection

cursor = db.cursor
 * 1) prepare a cursor object using //cursor// method

cursor.execute("DROP TABLE IF EXISTS EMPLOYEE")
 * 1) Drop table if it already exist using //execute// method.

sql = """CREATE TABLE EMPLOYEE (        FIRST_NAME  CHAR(20) NOT NULL,         LAST_NAME  CHAR(20),         AGE INT,         SEX CHAR(1),         INCOME FLOAT )"""
 * 1) Create table as per requirement

cursor.execute(sql)

db.close code || = INSERT Operation: = <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">INSERT operation is required when you want to create your records into a database table.
 * 1) disconnect from server

Example:
<span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">Following is the example which executes SQL //INSERT// statement to create a record into EMPLOYEE table. code <span style="font-family: 'Courier New',monospace; font-size: 12px;">#!/usr/bin/python

import MySQLdb

db = MySQLdb.connect("localhost","testuser","test123","TESTDB" )
 * 1) Open database connection

cursor = db.cursor
 * 1) prepare a cursor object using //cursor// method

sql = """INSERT INTO EMPLOYEE(FIRST_NAME,        LAST_NAME, AGE, SEX, INCOME)         VALUES ('Mac', 'Mohan', 20, 'M', 2000)""" try: # Execute the SQL command cursor.execute(sql) # Commit your changes in the database db.commit except: # Rollback in case there is any error db.rollback
 * 1) Prepare SQL query to INSERT a record into the database.

db.close code || <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">Above example can be written as follows to create SQL queries dynamically: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">#!/usr/bin/python
 * 1) disconnect from server

import MySQLdb

db = MySQLdb.connect("localhost","testuser","test123","TESTDB" )
 * 1) Open database connection

cursor = db.cursor
 * 1) prepare a cursor object using //cursor// method

sql = "INSERT INTO EMPLOYEE(FIRST_NAME, \      LAST_NAME, AGE, SEX, INCOME) \       VALUES ('%s', '%s', '%d', '%c', '%d' )" % \ ('Mac', 'Mohan', 20, 'M', 2000) try: # Execute the SQL command cursor.execute(sql) # Commit your changes in the database db.commit except: # Rollback in case there is any error db.rollback
 * 1) Prepare SQL query to INSERT a record into the database.

db.close code ||
 * 1) disconnect from server

Example:
<span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">Following code segment is another form of execute where you can pass parameters directly: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">.................................. user_id = "test123" password = "password"

con.execute('insert into Login values("%s", "%s")' % \            (user_id, password)) .................................. code || = READ Operation: = <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">READ Operation on any databasse means to fetch some useful information from the database. <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">Once our database connection is established, we are ready to make a query into this database. We can use either **fetchone** method to fetch single record or **fetchall** method to fetech multiple values from a database table. > > >
 * <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: left;"> **fetchone:** This method fetches the next row of a query result set. A result set is an object that is returned when a cursor object is used to query a table.
 * <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: left;"> **fetchall:** This method fetches all the rows in a result set. If some rows have already been extracted from the result set, the fetchall method retrieves the remaining rows from the result set.
 * <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: left;"> **rowcount:** This is a read-only attribute and returns the number of rows that were affected by an execute method.

Example:
<span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">Following is the procedure to query all the records from EMPLOYEE table having salary more than 1000. code <span style="font-family: 'Courier New',monospace; font-size: 12px;">#!/usr/bin/python

import MySQLdb

db = MySQLdb.connect("localhost","testuser","test123","TESTDB" )
 * 1) Open database connection

cursor = db.cursor
 * 1) prepare a cursor object using //cursor// method

sql = "SELECT * FROM EMPLOYEE \      WHERE INCOME > '%d'" % (1000) try: # Execute the SQL command cursor.execute(sql) # Fetch all the rows in a list of lists. results = cursor.fetchall for row in results: fname = row[0] lname = row[1] age = row[2] sex = row[3] income = row[4] # Now print fetched result print "fname=%s,lname=%s,age=%d,sex=%s,income=%d" % \ (fname, lname, age, sex, income ) except: print "Error: unable to fecth data"
 * 1) Prepare SQL query to INSERT a record into the database.

db.close code || <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">This will produce following result: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">fname=Mac, lname=Mohan, age=20, sex=M, income=2000 code || = Update Operation: = <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">UPDATE Operation on any databasse means to update one or more records which are already available in the database. Following is the procedure to update all the records having SEX as 'M'. Here we will increase AGE of all the males by one year.
 * 1) disconnect from server

Example:
code <span style="font-family: 'Courier New',monospace; font-size: 12px;">#!/usr/bin/python

import MySQLdb

db = MySQLdb.connect("localhost","testuser","test123","TESTDB" )
 * 1) Open database connection

cursor = db.cursor
 * 1) prepare a cursor object using //cursor// method

sql = "UPDATE EMPLOYEE SET AGE = AGE + 1                         WHERE SEX = '%c'" % ('M') try: # Execute the SQL command cursor.execute(sql) # Commit your changes in the database db.commit except: # Rollback in case there is any error db.rollback
 * 1) Prepare SQL query to UPDATE required records

db.close code || = DELETE Operation: = <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">DELETE operation is required when you want to delete some records from your database. Following is the procedure to delete all the records from EMPLOYEE where AGE is more than 20.
 * 1) disconnect from server

Example:
code <span style="font-family: 'Courier New',monospace; font-size: 12px;">#!/usr/bin/python

import MySQLdb

db = MySQLdb.connect("localhost","testuser","test123","TESTDB" )
 * 1) Open database connection

cursor = db.cursor
 * 1) prepare a cursor object using //cursor// method

sql = "DELETE FROM EMPLOYEE WHERE AGE > '%d'" % (20) try: # Execute the SQL command cursor.execute(sql) # Commit your changes in the database db.commit except: # Rollback in case there is any error db.rollback
 * 1) Prepare SQL query to DELETE required records

db.close code || = Performing Transactions: = <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">Transactions are a mechanism that ensures data consistency. Transactions should have the following four properties: > > > > <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">The Python DB API 2.0 provides two methods to either //commit// or //rollback// a transaction.
 * 1) disconnect from server
 * <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: left;"> **Atomicity:** Either a transaction completes or nothing happens at all.
 * <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: left;"> **Consistency:** A transaction must start in a consistent state and leave the system is a consistent state.
 * <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: left;"> **Isolation:** Intermediate results of a transaction are not visible outside the current transaction.
 * <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: left;"> **Durability:** Once a transaction was committed, the effects are persistent, even after a system failure.

Example:
<span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">You already have seen how we have implemented transations. Here is again similar example: code <span style="font-family: 'Courier New',monospace; font-size: 12px;"># Prepare SQL query to DELETE required records sql = "DELETE FROM EMPLOYEE WHERE AGE > '%d'" % (20) try: # Execute the SQL command cursor.execute(sql) # Commit your changes in the database db.commit except: # Rollback in case there is any error db.rollback code || = COMMIT Operation: = <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">Commit is the operation which gives a green signal to database to finalize the changes and after this operation no change can be reverted back. <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">Here is a simple example to call **commit** method. code <span style="font-family: 'Courier New',monospace; font-size: 12px;">   db.commit code || = ROLLBACK Operation: = <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">If you are not satisfied with one or more of the changes and you want to revert back those changes completely then use **rollback** method. <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">Here is a simple example to call **rollback** metho. code <span style="font-family: 'Courier New',monospace; font-size: 12px;">  db.rollback code || = Disconnecting Database: = <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">To disconnect Database connection, use close method. code <span style="font-family: 'Courier New',monospace; font-size: 12px;">   db.close code || <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">If the connection to a database is closed by the user with the close method, any outstanding transactions are rolled back by the DB. However, instead of depending on any of DB lower level implementation details, your application would be better off calling commit or rollback explicitly. = Handling Errors: = <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">There are many sources of errors. A few examples are a syntax error in an executed SQL statement, a connection failure, or calling the fetch method for an already canceled or finished statement handle. <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">The DB API defines a number of errors that must exist in each database module. The following table lists these exceptions. <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">Your Python scripts should handle these errors but before using any of the above exceptions, make sure your MySQLdb has support for that exception. You can get more information about them by reading the DB API 2.0 specification.
 * ~ Exception ||~ Description ||
 * Warning || Used for non-fatal issues. Must subclass StandardError. ||
 * Error || Base class for errors. Must subclass StandardError. ||
 * InterfaceError || Used for errors in the database module, not the database itself. Must subclass Error. ||
 * DatabaseError || Used for errors in the database. Must subclass Error. ||
 * DataError || Subclass of DatabaseError that refers to errors in the data. ||
 * 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. ||
 * IntegrityError || Subclass of DatabaseError for situations that would damage the relational integrity, such as uniqueness constraints or foreign keys. ||
 * InternalError || Subclass of DatabaseError that refers to errors internal to the database module, such as a cursor no longer being active. ||
 * ProgrammingError || Subclass of DatabaseError that refers to errors such as a bad table name and other things that can safely be blamed on you. ||
 * NotSupportedError || Subclass of DatabaseError that refers to trying to call unsupported functionality. ||

<span style="background-color: #ffffff; color: #900b09; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: left;"> <span style="background-color: #ffffff; color: #900b09; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: left;"> <span style="background-color: #ffffff; color: #900b09; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: left;">