From a construction firm to a stock exchange, every organisation depends on large databases. These are essentially collections of tables, and connected with each other through columns.
These database systems support SQL, the Structured Query Language, which is used to create, access and manipulate the data. SQL is used to access data, and also to create and exploit the relationships between the stored data. Additionally, these databases support database normalisation rules for avoiding redundancy of data.
The Python programming language has powerful features for database programming. Python supports various databases like MySQL, Oracle, Sybase, PostgreSQL, etc. Python also supports Data Definition Language (DDL), Data Manipulation Language (DML) and Data Query Statements.
For database programming, the Python DB API is a widely used module that provides a database application programming interface.
Benefits of Python for database programming
There are many good reasons to use Python for progran1ming database applications:
•Programming in Python is arguably more efficient and faster compared to other languages.
•Python is famous for its portability.
•It is platform independent.
•Python supports SQL cursors.
•In many programming languages, the application developer needs to take care of the open and closed connections of the database, to avoid further exceptions and errors. In Python, these connections are taken care of Python supports relational database systems. Python database APIs are compatible with various databases, so it is very easy to migrate and port database application interfaces.
DB-API (SQL-API) for Python
Python DB-API is independent of any database engine, which enables you to write Python scripts to access any database engine. The Python DB API in1plementation for MySQL is MySQLdb. For PostgreSQL, it supports psycopg, PyGresQL and pyPgSQL modules. DB-API implementations for Oracle are dc_oracle2 and cx_oracle. Pydb2 is the DB-API implementation for DB2. Pythons DB-API consists of connection objects, cursor objects, standard exceptions and some other module contents, all of which we will discuss.
Connection objects
Connection objects create a connection with the database and these are further used for different transactions. These connection objects are also used as representatives of the database session.
Cursor objects
Cursor is one of the powerful features of SQL. These are objects that are responsible for submitting various SQL statements to a database server. There are several cursor classes in MySQLdb. cursors:
1.BaseCursor is the base class for Cursor objects.
2.Cursor is the default cursor class. CursorWamingMixIn, CursorStoreResultMixIn, CursorTupleRowsMixIn, and BaseCursor are some components of the cursor class.
3.CursorStoreResultMixIn uses the mysqtstoreJesultO function to retrieve result sets from the executed query. These result sets are stored at the client side.
4.CursorUseResultMixIn uses the mysqtuseJesultO function to retrieve result sets from the executed query. These result sets are stored at the server side.
Error and exception handling in DB-API
Exception handling is very easy in the Python DB-API module. We can place warnings and error handling messages in the programs. Python DB-API has various options to handle this, like Warning; InterfaceError, DatabaseError, IntegrityError, IntemalError, NotS upported Error, OperationalError and Programming Error.
Python and MySQL
Python and MySQL are a good combination to develop database applications. After starting the MySQL service on Linux, you need to acquire MySQLdb. a Python DB-API for MySQL to perform database operations. You can check whether the MySQLdb module is installed in your system.




Reply With Quote
Copyright Techfuels
Bookmarks