Question 1: I have an SQLite3 connection in Python. How can I checked whether it is connected or not? I know that if sqlite3.connect() fails, an exception is raised, but if I or some closes the connection, how can I check this and reopen it if necessary?
Question 2: I can move the file in the file system while the connection is open (deletion is not possible). The database then becomes readonly, for whatever reason. If I move it back, it works as if nothing happened. Can anybody explain this? Should I check isfile(dbpath) before access?
"try ... except" works pretty well too
import sqlite3 as mdb
def chk_conn(conn):
try:
conn.cursor()
return True
except Exception as ex:
return False
myconn = mdb.connect('test.db')
print(chk_conn(myconn))
Out: True
myconn.close()
print(chk_conn(myconn))
Out: False
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With