在Python脚本中无法在本地数据库上进行Firebird连接 [英] Firebird connection on a local database impossible within a Python script
问题描述
我可以使用Firebird ISQL工具(Firebird 3.0.4)通过以下命令连接到我的Firebird数据库:
connect "C:\Documents\database.db" user 'USER' password 'PASSWORD';
当我想在Python脚本(Windows10 64位版本上为Python v3.7.7)上,在包括fdb v2.0.1甚至firebirdsql v1.1.3的虚拟环境中执行此操作时,我不能,而且我系统地获得了错误.
import fdb
con = fdb.connect(database="C:\Documents\database.db", user='USER' password='PASSWORD'')
DatabaseError :('连接数据库时出错:\ n- SQLCODE: -902 \ n-无法完成主机"xnet://Global \ FIREBIRD"的网络请求.',-902,335544721)
或
con = fdb.connect(host='localhost', database="D:\Documents\database.db", user= 'USER' password= 'PASSWORD'')
DatabaseError:((连接数据库时出错:\ n- SQLCODE: -902 \ n-无法完成对主机"localhost"的网络请求.\ n-无法建立连接.',-902,335544721)
或
con = fdb.connect(dsn="localhost:C:\Documents\database.db", user='USER' password='PASSWORD'')
DatabaseError:((连接数据库时出错:\ n- SQLCODE: -902 \ n-无法完成对主机"localhost"的网络请求.\ n-无法建立连接.',-902,335544721)
或
import firebirdsql
con = firebirdsql.connect(host='localhost', database="D:\Documents\database.db", user='USER' password='PASSWORD'')
如果您有任何想法,欢迎您,因为我被困住了.
错误表明FDB加载的fbclient.dll
不提供Firebird Embedded,并且您的计算机上没有运行Firebird Server.>
要解决此问题,您必须:
- 启动Firebird服务器(通过启动其服务或运行
firebird -a
) - 如果要使用Firebird Embedded而不是Firebird Server,请将FDB指向提供Firebird Embedded的
fbclient.dll
第2点可以通过多种方式完成.在我的回答中,我假设使用安装在C:\Program Files\Firebird\Firebird-3.0.5.33220-0_x64
中的Firebird 3.从Firebird 3开始,正常的Firebird Server安装也提供了Firebird Embedded.要指向嵌入式Firebird,您可以执行以下操作:
- 将Firebird安装目录添加到
PATH
环境变量(确保在之前%SystemRoot\System32
/C:\Windows\System32
列出).在正常的Firebird安装中,System32
文件夹中安装了没有Firebird Embedded的fbclient.dll
,并且如果该文件夹被加载,则不能使用Firebird Embedded. - 使用
fdb.load_api
加载客户端库:fdb.load_api('C:/Program Files/Firebird/Firebird-3.0.5.33220-0_x64/fbclient.dll')
这需要在第一次使用
fdb.connect
之前完成,否则将使用通过常规搜索路径找到的库 - 使用
fb_library_name
连接属性指定客户端库: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')
需要在使用FDB建立的第一个连接上指定此属性.尽管该属性的存在表明这是每个连接",但FDB将始终使用加载的第一个客户端库(本质上,它的工作方式就像您在
connect
之前调用load_api
一样).
如果您使用的是Firebird 2.5或更早版本,则需要下载特定的Firebird 2.5 Embedded软件包,并指向其fbembed.dll
而不是fbclient.dll
.对于Firebird 2.5 Embedded,除非将其fbembed.dll
重命名或复制到fbclient.dll
,否则无法将其位置添加到路径中.
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.
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: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 - 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 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
.
这篇关于在Python脚本中无法在本地数据库上进行Firebird连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!