Even better: try: sql = '''SELECT hits FROM counters WHERE url = %s''' % page print( sql ) cur.execute( sql ) data = cur.fetchone() except MySQLdb.ProgrammingError as e: print ( "Query Error: ", dir( sys.exc_info()[1] ) ) ======== sql statement seems okey...