Python Forum
Does Python sqlite3 detect connection errors
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Does Python sqlite3 detect connection errors
#1
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 None
I 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
Reply
#2
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/...lite3.html for information on exception processing. (there are examples)
Reply
#3
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.
Reply
#4
Please post exception, error traceback complete and unmodified (you should always do this).
Quote:My code clearly tries to capture the exception
when 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
Reply
#5
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
Reply
#6
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.
Reply
#7
Thank you
ZA
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I don't know what is wrong (Python and SQL connection) shereen 3 323 Apr-01-2024, 08:56 AM
Last Post: Pedroski55
  No Internet connection when running a Python script basil_555 8 577 Mar-11-2024, 11:02 AM
Last Post: snippsat
  Connection LTspice-Python with PyLTSpice bartel90 0 335 Feb-05-2024, 11:46 AM
Last Post: bartel90
  Virtual Env changing mysql connection string in python Fredesetes 0 370 Dec-20-2023, 04:06 PM
Last Post: Fredesetes
  connection python and SQL dawid294 4 653 Dec-12-2023, 08:22 AM
Last Post: Pedroski55
  When does Python detect Errors? stamp1t 1 439 Oct-21-2023, 05:53 PM
Last Post: deanhystad
  How to detect abnormal data in big database python vanphuht91 5 1,128 Jun-27-2023, 11:22 PM
Last Post: Skaperen
  Networking Issues - Python GUI client and server connection always freezes Veritas_Vos_Liberabit24 0 717 Mar-21-2023, 03:18 AM
Last Post: Veritas_Vos_Liberabit24
  Python MYSQL connection does not work after 1h idle zazas321 9 6,753 Oct-07-2021, 12:02 PM
Last Post: ndc85430
  Serial connection connection issue Joni_Engr 15 8,024 Aug-30-2021, 04:46 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020