UNIT-V Data Programming
UNIT-V Data Programming
UNIT - V
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.
A connection is created as follows:
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. CursorWarningMixIn, CursorStoreResultMixIn, CursorTupleRo
wsMixIn, and BaseCursor are some components of the cursor class.
3. CursorStoreResultMixIn uses the mysql_store_result() function to
retrieve result sets from the executed query. These result sets are
stored at the client side.
4. CursorUseResultMixIn uses the mysql_use_result() function to
retrieve result sets from the executed query. These result sets are
stored at the server side.
The following example illustrates the execution of SQL commands using cursor
objects. You can use execute to execute SQL commands like SELECT. To
commit all SQL operations you need to close the cursor as cursor.close().
>>>cursor.close()
The first two items (name and type_code) are mandatory, the other five are
optional and are set to None if no meaningful values can be provided.
Cursor Methods
This method is optional since not all databases provide stored procedures.
.callproc(procname [, parameters ]): It calls a stored database procedure with
the given name. The sequence of parameters must contain one entry for each
argument that the procedure expects.
.close(): Close the cursor now. The cursor will be unusable from this point
forward; an Error (or subclass) exception will be raised if any operation is
attempted with the cursor.
>>>import MySQLdb
If this command runs successfully, you can now start writing scripts for your
database.
To write database applications in Python, there are five steps to follow:
1. Import the SQL interface with the following command:
>>> conn=MySQLdb.connect(host='localhost',user='root',passwd='')
…where host is the name of your host machine, followed by the
username and password. In case of the root, there is no need to
provide a password.
3. Create a cursor for the connection with the following command:
>>>cursor = conn.cursor()
4. Execute any SQL query using this cursor as shown below—here the
outputs in terms of 1L or 2L show a number of rows affected by this
query:
(AIML & DS) Database Programming
DEPARTMENT OF COMPUTER SCIENCEAND ENGINEERING
0L
11. Finally, fetch the result set and iterate over this result set. In this
step, the user can fetch the result sets as shown below:
3. lL // Rows affected.
4. >>> cursor.execute('insert books values
(%s,%s,%s,%s)',('Py9099','Programming With Python',100,50))
1L //Rows affected.
If the user wants to insert duplicate entries for a book’s accession
number, the Python DB-API will show an error as it is the primary
key. The following example illustrates this:
5. The Update SQL query can be used to update existing records in the
database as shown below:
((170.0, 85.0),)
Data Attributes
apilevel
This string (not float) indicates the highest version of the DB-API the module is
compliantwith,i.e.,"1.0", "2.0", etc. If absent,"1.0" should be assumed as the
default value.
Threadsafety
Function Attribute(s)