Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="http://mysql-python.sourceforge.net/MySQLdb-1.2.2/private/_mysql_exceptions-module.html" rel="noreferrer">This link</a> shows the various types of Errors that you can catch. <code>MySQLdb.Error</code> is the standard base class from which all other MySQL Errors are derived.</p> <p>I usually use <code>MySQLdb.Error</code> because it lets you focus on errors relating to MySQLdb itself. By contrast <code>StandardError</code> will catch almost all the exceptions (not something you want if you want better debugging capability). Plus the use of <code>MySQLdb.Error</code> allows you to display the exact error message (<strong>MySQL</strong> error number and all) so that you can debug it faster.</p> <p>Coming to the first part of the question, in case of database statements it is (usually) necessary to rollback transactions (if they are supported) in case of error.</p> <p>The methodology that I follow is to wrap each <code>execute</code> statement in a try except clause (catching <code>MySQLdb.Error</code>) and using rollback if there is an an error before printing the error message and exiting. </p> <p>However, there is a catch. In MySQLdb the changes that you make to DB are <strong>not</strong> actually written to the database until you explicilty call commit. So, logically, rollback is not necessary. </p> <p>As an example,</p> <pre><code>conn = MySQLdb.connection(db=, host=, passwd=, user=) cur = conn.cursor() #Say you have a table X with one entry id = 1 and total = 50 cur.execute("update X set total = 70 where id = 1") #Actual DB has not yet changed cur.execute("update X set total = 80 where id = 1") #Actual DB has still not changed </code></pre> <p>If you exit the program <em>without</em> commiting, the value in DB will still be 50 because you never called commit().</p> <p>This is how you would ideally do it:</p> <pre><code>conn = MySQLdb.connection(db=, host=, passwd=, user=) cur = conn.cursor() #Say you have a table X with one entry id = 1 and total = 50 try: cur.execute("update X set total = 70 where id = 1") except MySQLdb.Error,e: print e[0], e[1] conn.rollback() cur.close() conn.close() #print lengthy error description!! sys.exit(2) #Note: Value in table is still 50 #If you do conn.commit() here, value becomes 70 in table too!! try: cur.execute("update X set total = 80 where id = 1") except MySQLdb.Error,e: print e[0], e[1] conn.rollback() cur.close() conn.close() #print lengthy error description!! sys.exit(2) #Value in DB will be #a) 50 if you didn't commit anywhere #b) 70 if you committed after first execute statement conn.commit() #Now value in DB is 80!! cur.close() conn.close() </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload