Print Mysql Error Python
Contents |
is 'module.Error'. In MySQL it is 'MySQLdb.Error'. Every DB-API
Pymysql Try Except
statement that you execute has the potential to raise an exception. So any time you execute a database query you should surround it mysql connector error 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
Mysqldb Execute
the workings and policies of this site About Us Learn more
Python Mysql Insert
about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow mysql.connector python 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 http://www.mikusa.com/python-mysql-docs/exceptions.html each other. Join them; it only takes a 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 http://stackoverflow.com/questions/30996401/error-handling-in-python-mysql the right value back. However, if the value is not stored in 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.
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 http://stackoverflow.com/questions/21721109/correct-exception-handling-with-python-mysqldb-connection 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 https://www.packtpub.com/books/content/exception-handling-mysql-python 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 Correct exception handling with python MySQLdb mysql error connection up vote 6 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 pymysql error handling I have been getting "Too many 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 wi
now Close Packt Logo Books & Videos Tech Packt Support Search My Collection Cart Book & Video Categories Web DevelopmentBig Data & Business IntelligenceApplication DevelopmentVirtualization & CloudGame DevelopmentNetworking & ServersHardware & CreativeBusiness All ProductsAll BooksAll VideosLatest Releases 50%off Deal of the Day A fast-paced guide to get you up and running with Swift 3 and its new features €13.12 RRP €26.23 Sign up here to get these deals straight to your inbox Tech Android AngularJS Arduino Blender Bootstrap Data Analysis Django Docker Drupal Graphics Programming Hadoop Internet of Things iOS Java JavaScript jQuery Linux Machine Learning OpenCV OpenStack Oracle Penetration Testing PHP PostgreSQL Python QlikView R Scala Spark Spring Swift Unity WordPress Packt Publishing About Us Our Authors Trade & Partners Contact Us Careers Support FAQs Ordering Information Terms & Conditions Code Downloads & Errata Submit Errata Mapt Subscribe Mapt For Business Take me to Mapt Search Added to Cart ...and more You don't have anything in your cart right now. Subtotal 0.00 Free shipping on print orders for US, UK, Europe and selected Asian countries View Cart Check Out Title added to cart Home > Books > MySQL for Python Exception Handling in MySQL for Python Albert Lukaszewski, PhD September 2010 MySQL for Python Integrate the flexibility of Python and the power of MySQL to boost the productivity of your Python applications Implement the outstanding features of Python's MySQL library to their full potential See how to make MySQL take the processing burden from your programs Learn how to employ Python with MySQL to power your websites and desktop applications Apply your knowledge of MySQL and Python to real-world problems instead of hypothetical scenarios A manual packed with step-by-step exercises to integrate your Python applications with the MySQL database server In the previous article, Inserting Multiple Entries with MySQL for Python, we learned about exception handling in Py