Python Mysql Error
Contents |
is 'module.Error'. In MySQL it is 'MySQLdb.Error'. Every DB-API
Python Database Error
statement that you execute has the potential to raise an exception. So any time you execute a database query you should surround it exception in mysql stored procedure with a try/exception block. This includes connections, cursor requests, and statement executions. # Get data from database try: cur.execute("SELECT * FROM song") rows = cur.fetchall() except MySQLdb.Error, e: try: print "MySQL Error [%d]: %s" % (e.args[0], e.args[1]) except IndexError: print "MySQL Error: %s" % str(e) # Print results in comma delimited format for row in rows: for col in row: print "%s," % col print "\n" <-- Home -->
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn mysqldb execute more about Stack Overflow the company Business Learn more about hiring developers or posting
Mysqldb Documentation
ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community
Python Mysql Insert
Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Correct exception handling with python MySQLdb connection up vote 6 http://www.mikusa.com/python-mysql-docs/exceptions.html down vote favorite 1 I created a small/basic python script to insert data into a MySQL database. I included some error handling - mainly to close the connection and/or prevent hanging connections in the case of an error (...but also to ignore some errors). I thought what I had (see below) was right - it seemed to be working okay. But occasionally I have been getting "Too many http://stackoverflow.com/questions/21721109/correct-exception-handling-with-python-mysqldb-connection connection" errors - which I assumes means I am not actually closing the connection correctly at all (or perhaps error handling isn't right). conn=MySQLdb.connect(host=####, user=####, passwd=####, db=####) curs=conn.cursor() try: curs.execute(sql) conn.commit() except MySQLdb.Error as e: if e[0]!= ###: raise finally: curs.close() conn.close() (I also tried without finally:) The other (I think important) point is that it is that the MySQL database uses an InnoDB storage engine. This is the first time I have used InnoDB engine and perhaps there are some differences to MyISAM that are relevant here, that I am not aware of (like conn.commit(), but for an error).... That seems to be the source of all my other problems! Thanks in advance python error-handling innodb mysql-python share|improve this question edited Feb 14 '14 at 3:34 asked Feb 12 '14 at 7:15 djmac 1591413 You should remove the conn.close() from the try body, as it will always be called within the finally body. Other than that, it looks like you're releasing your connections properly. –lanzz Feb 12 '14 at 7:51 Thanks Lanzz - sorry I made an error copying the code (was playing around without finally:, and had a conn.close() in the try body and the except bod
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies http://stackoverflow.com/questions/30996401/error-handling-in-python-mysql of this site About Us Learn more about Stack Overflow the company http://stackoverflow.com/questions/5178292/pip-install-mysql-python-fails-with-environmenterror-mysql-config-not-found Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a mysql error minute: Sign up Error handling in Python-MySQL up vote 1 down vote favorite I am running a little webservice based on python flask, where I want to execute a small MySQL Query. When I get a valid input for my SQL query, everything is working as expected and I get the right value back. However, if the value is not stored in pymysql error handling the database I receive a TypeError Traceback (most recent call last): File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1836, in __call__ return self.wsgi_app(environ, start_response) File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1820, in wsgi_app response = self.make_response(self.handle_exception(e)) File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1403, in handle_exception reraise(exc_type, exc_value, tb) File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app response = self.full_dispatch_request() File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1478, in full_dispatch_request response = self.make_response(rv) File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1566, in make_response raise ValueError('View function did not return a response') ValueError: View function did not return a response I tried to tap into error handling myself and use this code for my project, but it seems like this doesn't work properly. #!/usr/bin/python from flask import Flask, request import MySQLdb import json app = Flask(__name__) @app.route("/get_user", methods=["POST"]) def get_user(): data = json.loads(request.data) email = data["email"] sql = "SELECT userid FROM oc_preferences WHERE configkey='email' AND configvalue LIKE '" + email + "%';"; conn = MySQLdb.connect( host="localhost", user="root", passwd="ubuntu", db="owncloud", port=3306) curs = conn.cursor() try: curs.execute(sql) user = curs.fetchone()[0] return user except MySQLdb.Error, e: try: print "MySQL Error [%d]: %s" % (e.args[0], e.args[1]) return None except IndexError: print "MySQL Error: %s" % str(e) return None ex
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up pip install mysql-python fails with EnvironmentError: mysql_config not found up vote 464 down vote favorite 127 This is the error I get (mysite)zjm1126@zjm1126-G41MT-S2:~/zjm_test/mysite$ pip install mysql-python Downloading/unpacking mysql-python Downloading MySQL-python-1.2.3.tar.gz (70Kb): 70Kb downloaded Running setup.py egg_info for package mysql-python sh: mysql_config: not found Traceback (most recent call last): File "