问题
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:
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:
- Start Firebird Server (by starting its service or running
firebird -a
) - 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:
- 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, thefbclient.dll
without Firebird Embedded is installed in theSystem32
folder, and if that gets loaded, you can't use Firebird Embedded. - Use
fdb.load_api
to load the client library:
This needs to be done before the first use offdb.load_api('C:/Program Files/Firebird/Firebird-3.0.5.33220-0_x64/fbclient.dll')
fdb.connect
, otherwise the library found through the normal search path will be used - Specify the client library using the
fb_library_name
connection property:
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 calledcon = 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')
load_api
just before theconnect
).
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
.
来源:https://stackoverflow.com/questions/62234154/firebird-connection-on-a-local-database-impossible-within-a-python-script