Firebird Driver for Python

2.0.2 · active · verified Thu Apr 16

firebird-driver is the official Python Database API 2.0-compliant driver for the open-source relational database Firebird®. It provides a pure-Python interface built on top of the native Firebird client library, exposing both the standard DB API features and the new interface-based client API introduced in Firebird 3. The library is currently at version 2.0.2 and maintains an active release cadence with regular updates and bug fixes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to establish a connection to a Firebird database, create a table (if it doesn't exist), insert data, and query data using the `firebird-driver`. It uses environment variables for database path, user, and password for security and flexibility. A local `test.fdb` will be created if `FIREBIRD_DB_PATH` is not set. Ensure the Firebird client library (`fbclient.dll` on Windows, `libfbclient.so` on Linux) is installed and accessible in your system's library path.

import os
from firebird.driver import connect, DatabaseError

database_path = os.environ.get('FIREBIRD_DB_PATH', 'test.fdb')
user = os.environ.get('FIREBIRD_USER', 'sysdba')
password = os.environ.get('FIREBIRD_PASSWORD', 'masterkey')

# Ensure the Firebird client library is accessible (e.g., in PATH or LD_LIBRARY_PATH)
# You might need to set fb_client_library in driver_config for specific paths.

try:
    # Connect to the Firebird database
    # For a remote server, specify 'host' and 'port' in addition to 'database'
    # E.g., con = connect('localhost:/path/to/my.fdb', user=user, password=password)
    con = connect(database_path, user=user, password=password)
    print("Successfully connected to Firebird database!")

    # Create a Cursor object
    cur = con.cursor()

    # Example: Create a table
    try:
        cur.execute("CREATE TABLE languages (name VARCHAR(20), year_released INTEGER)")
        print("Table 'languages' created.")
    except DatabaseError as e:
        if 'Table or view already exists' in str(e): # Specific error for existing table
            print("Table 'languages' already exists.")
        else:
            raise # Re-raise other database errors

    # Example: Insert data
    cur.execute("INSERT INTO languages (name, year_released) VALUES (?, ?)", ('Python', 1991))
    cur.execute("INSERT INTO languages (name, year_released) VALUES (?, ?)", ('C', 1972))
    con.commit()
    print(f"{cur.rowcount} rows inserted.")

    # Example: Query data
    cur.execute("SELECT name, year_released FROM languages ORDER BY year_released")
    print("\n--- Languages ---")
    for row in cur.fetchall():
        print(f"Name: {row[0]}, Released: {row[1]}")

    cur.close()
    con.close()
    print("Connection closed.")

except DatabaseError as e:
    print(f"Database error: {e}")
    print("Ensure Firebird server is running and the client library is configured correctly.")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →