Does Python sqlite3 detect connection errors - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Does Python sqlite3 detect connection errors (/thread-15450.html) |
Does Python sqlite3 detect connection errors - zatlas1 - Jan-17-2019 Hi all try: dbconn = sqlite3.connect('a_real_sqlite3_file') return dbconn except (sqlite3.Error, sqlite3.Warning) as e: common_func.eprint(common_func.TimeStamp() + e) return NoneI try his code when a_real_sqlite3_file is an actual SQLite file and all is good. If I replace that string with 'blabla' (which does not exist anywhere. I still get no exception and the first place I see an issue is later when I try to execute a select statement. Is that a normal behavior, or a lousy implementation? ZA RE: Does Python sqlite3 detect connection errors - Larz60+ - Jan-17-2019 It issues exceptions for errors, but you have to capture the exceptions and issue appropriate action based on severity. If a fatal error, you will get an error traceback, and program will abort (unless intercepted) See: https://python.readthedocs.io/en/latest/library/sqlite3.html for information on exception processing. (there are examples) RE: Does Python sqlite3 detect connection errors - zatlas1 - Jan-17-2019 My code clearly tries to capture the exception and there are other places in my code that actually capture exceptions for object.execute('sql statement') It looks like the people who'd developed the sqlite3 driver decided that failing to establish connection is NOT worthy to cause an exception. I guess that I have to conclude that it is a lousy implementation. RE: Does Python sqlite3 detect connection errors - Larz60+ - Jan-18-2019 Please post exception, error traceback complete and unmodified (you should always do this). Quote:My code clearly tries to capture the exceptionwhen an exception occurs If it's not being caught, your try/except clause is either in the wrong place, or you are not capturing the right exception. Very hard to say since you don't show the error you are getting RE: Does Python sqlite3 detect connection errors - zatlas1 - Jan-18-2019 OK, let me explain it clearly! I do NOT get any exception! despite the fact that the file 'a_real_sqlite3_file' (for example) does not exist. Therefore, there is nothing to show! I run it on Windows 10, under the PyCharm IDE. This code SHOULD have generated an exception but it does not! Thank you ZA RE: Does Python sqlite3 detect connection errors - woooee - Jan-18-2019 Try this instead of banging your head against the wall. From the docs "The example above causes the database file named "ex1.db" to be opened and used. The "ex1.db" file is created if it does not previously exist." Use os.path.isfile() the check for an existing file. RE: Does Python sqlite3 detect connection errors - zatlas1 - Jan-18-2019 Thank you ZA |