UNIT-V Data Programming
UNIT-V Data Programming
UNIT - V
There are many good reasons to use Python for programming database
applications:
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.
Rollbacks are important for database integrity because they mean that
the database can be restored to a clean copy even after erroneous
operations are performed.
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:
>>>cursor.close()
Cursors created from the same connection are not isolated, i.e., any changes
done to the database by a cursor are immediately visible by the other
cursors.
Cursor Attributes
Cursor Objects should respond to the following methods and attributes.
name
type_code
display_size
internal_size
precision
scale
null_ok
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.
.nextset(): This method will make the cursor skip to the next available set,
discarding any remaining rows from the current set. If there are no more
sets, the method returns None. Otherwise, it returns a true value, and
subsequent calls to the .fetch*() methods will return rows from the next
result set.
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, InternalError, Not
SupportedError, OperationalError and ProgrammingError.
Let’s take a look at them one by one:
4.>>>cursor.execute(q,[50])
11. File
"/usr/lib/python2.3/site-packages/MySQLdb/connections.py", line 33,
in defaulterrorhandler
22. File
"/usr/lib/python2.3/site-packages/MySQLdb/connections.py", line 33,
in defaulterrorhandler
>>>import MySQLdb
If this command runs successfully, you can now start writing scripts for your
database.
>>>
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.
>>>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:
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:
12. >>> cursor.execute('select * from books')
13. 2L
We can perform all SQL operations with Python DB-API. Insert, delete,
aggregate and update queries can be illustrated as follows.
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:
7.2L
9.2L
11. The Delete SQL query can be used to delete existing records in
the database as shown below:
13. 2L
15. 0L
17. ()
18.
20. 3L
23. 4L
27. 1L
((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
Notcursors
●3: Fully threadsafe: threads can share the module, connections, and
cursors
Paramstyle
printf() format
conversion WHERE
name=%(name)s
qmark Question mark style WHERE name=?
ANSI C printf() format
format WHERE name=%s
conversion
Function Attribute(s)
connect(dsn='myhost:MYDB',user='guido',password='234$')