Python Forum

Full Version: SQLite question
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
I am using SQLite (version 2.6.0)
Somewhere in the code I need to check if there is an active connection to a database.

In mySQL there is something like "conn.is_connected()".
This seems to be missing in SQLite, or did I miss it?
thx,
Pau

Edit: seems to be missing in SQLite. Then we'll do conn = None or not None.
It's been a while since I used the sqlite3 package, but I do have some functions that I will go back to, should I need them. As I have the functions in a directory that is my 'go-to' for things that I have written and tested, I see no reason why they would not work, even if some 'tinker time' is still needed.

You're welcome to try these and see if you can use them:

import sqlite3 as db
import os.path

path = '' # the full path needs to go here

#===========Database Functions===========#

def process_file(file, path=path):
    filename = os.path.join(path, file)
    try:
        f = open(filename, 'r')
    except FileNotFoundError:
        return file, 0
    else:
        f.close()
        return file, 1


def db_file(file_name):
    print(f"Testing connection...")
    database, test = process_file(file_name)
    if test:
        conn = db.connect(database)
        cur = conn.cursor()
        if conn:
            print(f"Connection to {database} established.")
            conn.close()
            print(f"Connection to {database} closed.")
    else:
        print(f"Alert!\n{database} database file not found.")
    print("Connection test complete.\n")
    return file_name, test
Note: Developed and tested on a Linux platform:
Python: 3.6.9
sqlite3: 2.6.0
(May-24-2023, 06:00 AM)DPaul Wrote: [ -> ]Hi,
I am using SQLite (version 2.6.0)
Somewhere in the code I need to check if there is an active connection to a database.

In mySQL there is something like "conn.is_connected()".
This seems to be missing in SQLite, or did I miss it?
thx,
Pau

Edit: seems to be missing in SQLite. Then we'll do conn = None or not None.

In SQLite, there is no built-in method like conn.is_connected() to check if there is an active connection to the database. However, you can use a try-except block to handle any exceptions that might occur when attempting to execute a query or interact with the database. If an exception is raised, it usually indicates that the connection is not active.

Here's an example of how you can check the connection status in SQLite using a try-except block:

import sqlite3

# Establish a connection to the SQLite database
connection = sqlite3.connect("your_database.db")

try:
    # Attempt to execute a simple query to check the connection
    cursor = connection.cursor()
    cursor.execute("SELECT 1")
    result = cursor.fetchone()
    if result[0] == 1:
        print("Connection is active")
    else:
        print("Connection is not active")
except sqlite3.Error as error:
    print("Connection is not active:", error)
finally:
    # Close the connection
    connection.close()
In this example, if the query successfully executes and returns a result of 1, it indicates that the connection is active. If an exception is raised, it means that the connection is not active. Finally, the connection.close() statement ensures that the connection is closed regardless of the outcome.
Thanks both for interesting approaches.
Now I know I don't have to look for a built-in method.

I already went the try-except route, but I'll tweak it a little
in view of the comments above.
thx,
Paul