{"id":203,"date":"2015-05-08T17:00:23","date_gmt":"2015-05-08T21:00:23","guid":{"rendered":"http:\/\/pfsensesetup.com\/pythonscript.net\/?p=203"},"modified":"2015-05-08T14:42:04","modified_gmt":"2015-05-08T18:42:04","slug":"python-database-programming-part-nine","status":"publish","type":"post","link":"http:\/\/pfsensesetup.com\/pythonscript.net\/python-database-programming-part-nine\/","title":{"rendered":"Python Database Programming: Part Nine"},"content":{"rendered":"<p><span style=\"text-decoration: underline;\"><strong><a href=\"http:\/\/pfsensesetup.com\/pythonscript.net\/wp-content\/uploads\/2015\/01\/python-logo.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-medium wp-image-114\" src=\"http:\/\/pfsensesetup.com\/pythonscript.net\/wp-content\/uploads\/2015\/01\/python-logo-300x98.png\" alt=\"Python Database Programming\" width=\"300\" height=\"98\" srcset=\"http:\/\/pfsensesetup.com\/pythonscript.net\/wp-content\/uploads\/2015\/01\/python-logo-300x98.png 300w, http:\/\/pfsensesetup.com\/pythonscript.net\/wp-content\/uploads\/2015\/01\/python-logo.png 610w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a>Python Database Programming: Committing and Rolling Back Transactions<\/strong><\/span><\/p>\n<p>Each connection, while it is engaged in action, manages a transaction. With SQL, data is not modified unless you commit a transaction. The database then guarantees that it will perform all of the modifications in the transaction or none. As a result, you will not leave your database in a potentially erroneous condition.<\/p>\n<p>To commit a transaction, we call the commit method of a connection:<\/p>\n<p><strong>conn.commit()<\/strong><\/p>\n<p>Note the the transaction methods are part of the connection class, not the cursor class.<\/p>\n<p>If something goes wrong, like an exception is thrown that you can handle, you should call the rollback method to undo the effects of the incomplete transaction; this will restore the database to the state it was in before you started the transaction, guaranteed:<\/p>\n<p><strong>conn.rollback()<\/strong><\/p>\n<p>The capability to roll back a transaction is very important, because you can handle errors by ensuring that the database does not get changed. In addition, rollbacks are very useful for testing. You can insert, modify and delete a number of rows as part of a unit test and then roll back the transaction to undo the effects of all the changes. This enables your unit tests to run without making any permanent changes to the database. It also enables your unit tests to be run repeatedly, because each run resets the data.<\/p>\n<p>The DB API defines several globals that need to be defined at the module level. You can use these globals to determine information about the database module and the features it supports. The following table lists these globals.<\/p>\n<table border=\"5\">\n<tbody>\n<tr>\n<th>Global<\/th>\n<th>What It Holds<\/th>\n<\/tr>\n<tr>\n<td>Apilevel<\/td>\n<td>Should hold &#8216;2.0&#8217; for the DB API 2.0, or &#8216;1.0&#8217; for the 1.0 API.<\/td>\n<\/tr>\n<tr>\n<td>Paramstyle<\/td>\n<td>Defines how you can indicate the placeholders for dynamic data in your SQL statements. The values include:<\/p>\n<ul>\n<li>&#8216;qmark&#8217;: Use question marks<\/li>\n<li>&#8216;numeric&#8217;: Use a positional number style; e.g. &#8216;:1&#8217;, &#8216;:2&#8217;, etc.<\/li>\n<li>&#8216;named&#8217;: Use a colon and a name for each parameter.<\/li>\n<li>&#8216;format&#8217;: Use the ANSI C sprintf format codes.<\/li>\n<li>&#8216;pyformat&#8217;: Use the Python extended format codes.<\/li>\n<\/ul>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>With a cursor object, you can check the definition attribute to see information about the data returned. This information should be a set of seven-element sequences, one for each column of result data. These sequences include the following items:<\/p>\n<p>(name, type_code, display_size, internal_size, precision, scale, null_ok)<\/p>\n<p>None can be used for all but the first two items, as shown in this example:<\/p>\n<p><strong>((&#8216;FIRSTNAME&#8217;, None, None, None, None, None, None),<br \/>\n(&#8216;LASTNAME&#8217;, None, None, None, None, None, None),<br \/>\n(&#8216;NAME&#8217;, None, None, None, None, None, None))<\/strong><\/p>\n<p>With databases, errors happen a lot. The DB API defines a number of errors that must exist in each database module. The following table lists those errors:<\/p>\n<table border=\"5\">\n<tbody>\n<tr>\n<th>Exception<\/th>\n<th>Usage<\/th>\n<\/tr>\n<tr>\n<td>Warning<\/td>\n<td>Used for non-fatal issues. Must subclass StandardError.<\/td>\n<\/tr>\n<tr>\n<td>Error<\/td>\n<td>Base class for errors. Must subclass StandardError.<\/td>\n<\/tr>\n<tr>\n<td>InterfaceError<\/td>\n<td>Used for errors in the database module, not the database itself. Must subclass Error.<\/td>\n<\/tr>\n<tr>\n<td>DatabaseError<\/td>\n<td>Used for errors in the database. Must subclass Error.<\/td>\n<\/tr>\n<tr>\n<td>DataError<\/td>\n<td>Subclass of Database error that refers to errors in the data.<\/td>\n<\/tr>\n<tr>\n<td>OperationalError<\/td>\n<td>Subclass of DatabaseError that refers to errors such as the loss of the connection to the database. These errors are generally outside of the control of a Python scripter.<\/td>\n<\/tr>\n<tr>\n<td>IntegrityError<\/td>\n<td>Subclass of DatabaseError for situations that would damage the relational integrity, such as uniqueness constraints or foreign keys.<\/td>\n<\/tr>\n<tr>\n<td>InternalError<\/td>\n<td>Subclass of DatabaseError that refers to errors internal to the database module, such as a cursor no longer being active.<\/td>\n<\/tr>\n<tr>\n<td>ProgrammingError<\/td>\n<td>Subclass of DatabaseError that refers to errors such as bad table name and other things that can safely be blamed on the scripter.<\/td>\n<\/tr>\n<tr>\n<td>NotSupportedError<\/td>\n<td>Subclass of DatabaseError that refers to trying to call unsupported functionality.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Your Python scripts should handle these errors. You can get more information about them by reading the DB API specification.<\/p>\n<h3><span style=\"text-decoration: underline;\"><strong>External Links:<\/strong><\/span><\/h3>\n<p><a href=\"https:\/\/wiki.python.org\/moin\/DatabaseProgramming\/\">Database Programming at wiki.python.org<\/a><\/p>\n<p><a href=\"https:\/\/wiki.python.org\/moin\/DatabaseProgramming\/\">Database Programming at python.about.com<\/a><\/p>\n<p><a href=\"http:\/\/docs.python-guide.org\/en\/latest\/scenarios\/db\/\">Databases at docs.python-guide.org<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python Database Programming: Committing and Rolling Back Transactions Each connection, while it is engaged in action, manages a transaction. With SQL, data is not modified unless you commit a transaction. The database then guarantees that it will perform all of the modifications in the transaction or none. As a result, you will not leave your [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,3],"tags":[],"class_list":["post-203","post","type-post","status-publish","format-standard","hentry","category-guides","category-tutorials","entry"],"_links":{"self":[{"href":"http:\/\/pfsensesetup.com\/pythonscript.net\/wp-json\/wp\/v2\/posts\/203","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/pfsensesetup.com\/pythonscript.net\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/pfsensesetup.com\/pythonscript.net\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/pfsensesetup.com\/pythonscript.net\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/pfsensesetup.com\/pythonscript.net\/wp-json\/wp\/v2\/comments?post=203"}],"version-history":[{"count":1,"href":"http:\/\/pfsensesetup.com\/pythonscript.net\/wp-json\/wp\/v2\/posts\/203\/revisions"}],"predecessor-version":[{"id":204,"href":"http:\/\/pfsensesetup.com\/pythonscript.net\/wp-json\/wp\/v2\/posts\/203\/revisions\/204"}],"wp:attachment":[{"href":"http:\/\/pfsensesetup.com\/pythonscript.net\/wp-json\/wp\/v2\/media?parent=203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/pfsensesetup.com\/pythonscript.net\/wp-json\/wp\/v2\/categories?post=203"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/pfsensesetup.com\/pythonscript.net\/wp-json\/wp\/v2\/tags?post=203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}