Python Catch Database Error
Contents |
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
Python Raise Runtime Error
site About Us Learn more about Stack Overflow the company Business Learn more python raise runtimeerror about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x
Python Exception Handling Best Practices
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 Correct way python raise runtime error example to handle exceptions when working with database up vote 0 down vote favorite 1 When I am executing a simple query (I am using tornado.database module) I handle exceptions like this: try: self.application.db.execute('DELETE FROM table_one WHERE a = 1') except Exception, e: logging.warning('DB exception: %s' % e) self.set_status(500) return If I want to do a transaction though, it would be smart to rollback it python pass exception to calling function in case of exception: try: self.application.db.execute('START TRANSACTION') self.application.db.execute('DELETE FROM table_one WHERE a = 1') self.application.db.execute('DELETE FROM table_two WHERE b = 2') self.application.db.execute('COMMIT') except Exception, e: logging.warning('DB exception: %s' % e) self.set_status(500) self.application.db.execute('ROLLBACK') return But what if rollback will cause an exception too (e.g. if connection fails)? Do I need to put a nested try-except block inside of the except block? python database exception exception-handling tornado share|improve this question asked Oct 5 '12 at 11:35 bazzilic 427414 To start with, don't catch the general Exception. Instead try to catch whatever specific error you are ready to handle. As for rollback, if the connection is lost then (any normal) database will rollback automatically. –Some programmer dude Oct 5 '12 at 11:41 @JoachimPileborg tornado throws general Exception. Yes, it will, but default wait_timeout for MySQL is 28800 seconds, so other transactions on these tables would be blocked for that period of time, so I need to rollback explicitly. –bazzilic Oct 5 '12 at 11:48 add a comment| 2 Answers 2 active oldest votes up vote 1 down vote accepted Placing a nested try ... except block in except
here for a quick overview of the site Help Center Detailed answers
Python Raise Exception Without Traceback
to any questions you might have Meta Discuss the workings python excepthook and policies of this site About Us Learn more about Stack Overflow the company Business Learn
Python Runtime Error Example
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 http://stackoverflow.com/questions/12745455/correct-way-to-handle-exceptions-when-working-with-database 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 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 http://stackoverflow.com/questions/30996401/error-handling-in-python-mysql 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 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=["POS
Connectors More MySQL.com Downloads Developer https://www.packtpub.com/books/content/exception-handling-mysql-python Zone Section Menu: Documentation Home MySQL Connector/Python Developer Guide Preface and Legal Notices Introduction to MySQL Connector/Python Guidelines for Python Developers Connector/Python python raise Versions Connector/Python Installation Connector/Python Coding Examples Connector/Python Tutorials Connector/Python Connection Establishment The Connector/Python C Extension Connector/Python Other Topics Connector/Python API Reference mysql.connector Module mysql.connector.connect() Method mysql.connector.apilevel Property mysql.connector.paramstyle Property python raise runtime mysql.connector.threadsafety Property mysql.connector.__version__ Property mysql.connector.__version_info__ Property connection.MySQLConnection Class connection.MySQLConnection() Constructor MySQLConnection.close() Method MySQLConnection.commit() Method MySQLConnection.config() Method MySQLConnection.connect() Method MySQLConnection.cursor() Method MySQLConnection.cmd_change_user() Method MySQLConnection.cmd_debug() Method MySQLConnection.cmd_init_db() Method MySQLConnection.cmd_ping() Method MySQLConnection.cmd_process_info() Method MySQLConnection.cmd_process_kill() Method MySQLConnection.cmd_query() Method MySQLConnection.cmd_query_iter() Method MySQLConnection.cmd_quit() Method MySQLConnection.cmd_refresh() Method MySQLConnection.cmd_reset_connection() Method MySQLConnection.cmd_shutdown() Method MySQLConnection.cmd_statistics() Method MySQLConnection.disconnect() Method MySQLConnection.get_row() Method MySQLConnection.get_rows() Method MySQLConnection.get_server_info() Method MySQLConnection.get_server_version() Method MySQLConnection.is_connected() Method MySQLConnection.isset_client_flag() Method MySQLConnection.ping() Method MySQLConnection.reconnect() Method MySQLConnection.reset_session() Method MySQLConnection.rollback() Method MySQLConnection.set_charset_collation() Method MySQLConnection.set_client_flags() Method MySQLConnection.shutdown() Method MySQLConnection.start_transaction() Method MySQLConnection.autocommit Property MySQLConnection.can_consume_results Property MySQLConnection.charset Property MySQLConnection.collation Property MySQLConn
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 €12.49 RRP €24.97 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 Python. This article, Albert Lukaszewski, PhD, author of MySQL for Python, is a sequel o