Paul's Programming Notes     Archive     Feed     Github

Error Handling In mysqldb - Python

I've had issues with queries failing and leaving connections open (enough to stall a server...). I know python's oursql library supports using the WITH keyword, and I think it will close the connection when there is an unexpected error. However, I'm not sure if I'm ready to move to a different library for MySQL (it's working well).

Here's what I'm currently doing to close the cursor and connection, then re-raise the error:

import MySQLdb

conn = MySQLdb.connect(user="username", passwd="secret", db="database", charset='utf8')
cur = conn.cursor()

try:
cur.execute("INSERT INTO testTable (userid) VALUES(%s);" % id)
conn.commit()
except:
cur.close()
conn.close()
raise
finally:
print "Insert Successful"

haufe.sharepoint Python Library - URLs With %20

I changed line 25 of the haufe.sharepoint library to fix an issue I was having with URLs which had %20 (the url encoded space character):

New Code:
location = urllib.quote(self.location(), safe=":/")

Old Code:
location = self.location()

Just try using a URL with %20 with haufe.sharepoint to see what I'm talking about.

urllib2.URLError Python SUDS library

To figure this out, you're going to need logging. Turn on logging with the following:

http://stackoverflow.com/questions/2388046/can-you-help-me-solve-this-suds-soap-issue

import logging logging.basicConfig(level=logging.INFO) logging.getLogger('suds.client').setLevel(logging.DEBUG) logging.getLogger('suds.transport').setLevel(logging.DEBUG) logging.getLogger('suds.xsd.schema').setLevel(logging.DEBUG) logging.getLogger('suds.wsdl').setLevel(logging.DEBUG)

MySQL IF Statement Affect On Speed

if(`table`.`start`='0000-00-00','',`table`.`start`)

That's the part of my query that was increasing the execution time by 10x. 

I changed it to just `table`.`start` (without the IF statement) and the query was 10x faster.