Firebird connection on a local database impossible within a Python script

4.6k Views Asked by At

I can connect to my Firebird database using Firebird ISQL Tool (Firebird 3.0.4) with the following command: connect "C:\Documents\database.db" user 'USER' password 'PASSWORD';

When I want to do it in a Python script (Python v3.7.7 on a Windows10 64 bits), in a virtual environment including fdb v2.0.1 or even firebirdsql v1.1.3, I can't and I systematically get an error.

import fdb
con = fdb.connect(database="C:\Documents\database.db", user='USER' password='PASSWORD'')

DatabaseError: ('Error while connecting to database:\n- SQLCODE: -902\n- Unable to complete network request to host "xnet://Global\FIREBIRD".', -902, 335544721)

or

con = fdb.connect(host='localhost', database="D:\Documents\database.db", user= 'USER' password= 'PASSWORD'')

DatabaseError: ('Error while connecting to database:\n- SQLCODE: -902\n- Unable to complete network request to host "localhost".\n- Failed to establish a connection.', -902, 335544721)

or

con = fdb.connect(dsn="localhost:C:\Documents\database.db", user='USER' password='PASSWORD'')

DatabaseError: ('Error while connecting to database:\n- SQLCODE: -902\n- Unable to complete network request to host "localhost".\n- Failed to establish a connection.', -902, 335544721)

or

import firebirdsql
con = firebirdsql.connect(host='localhost', database="D:\Documents\database.db", user='USER' password='PASSWORD'')

If you have any idea you are welcome as I am stuck.

1

There are 1 best solutions below

4
On BEST ANSWER

The error indicates that the fbclient.dll loaded by FDB does not provide Firebird Embedded, and that you don't have Firebird Server running on your machine.

To address this you must either:

  1. Start Firebird Server (by starting its service or running firebird -a)
  2. If you want to use Firebird Embedded instead of Firebird Server, point FDB to a fbclient.dll that provides Firebird Embedded

Point 2 can be done in several ways. In my answer I'm assuming use of Firebird 3 installed in C:\Program Files\Firebird\Firebird-3.0.5.33220-0_x64. Since Firebird 3, a normal Firebird Server install also provides Firebird Embedded. To point to a Firebird Embedded, you can do the following:

  1. Add the Firebird installation directory to the PATH environment variable (make sure it is listed before %SystemRoot\System32/C:\Windows\System32). On a normal Firebird installation, the fbclient.dll without Firebird Embedded is installed in the System32 folder, and if that gets loaded, you can't use Firebird Embedded.
  2. Use fdb.load_api to load the client library:
    fdb.load_api('C:/Program Files/Firebird/Firebird-3.0.5.33220-0_x64/fbclient.dll')
    
    This needs to be done before the first use of fdb.connect, otherwise the library found through the normal search path will be used
  3. Specify the client library using the fb_library_name connection property:
    con = fdb.connect(dsn='C:/path/to/yourdatabase.fdb', user='sysdba', password='masterkey', 
        fb_library_name='C:/Program Files/Firebird/Firebird-3.0.5.33220-0_x64/fbclient.dll')
    
    This property needs to be specified on the first connection made using FDB. Although the existence of the property would suggest this is 'per connection', FDB will always use the first client library loaded (in essence, it works as if you called load_api just before the connect).

If you're using Firebird 2.5 or earlier, you will need to download the specific Firebird 2.5 Embedded package, and point to its fbembed.dll instead of fbclient.dll. For Firebird 2.5 Embedded, adding its location to the path will not work unless you rename or copy its fbembed.dll to fbclient.dll.