简体   繁体   English

如何使用 FDB 在 Python 脚本中恢复 Firebird 数据库?

[英]How to restore a Firebird database in a Python script using FDB?

Firebird 3.0.4 is installed and Python v3.7.7 on a Windows10 64 bits system.在 Windows10 64 位系统上安装 Firebird 3.0.4 和 Python v3.7.7。

I can restore a database with the following command:我可以使用以下命令恢复数据库:

gbak.exe -r -USER user -PASSWORD password database.fdk database.fdb

I would like to do the same using fdb (Firebird Embedded) in a Python script but it does not work!我想在 Python 脚本中使用 fdb(Firebird Embedded)做同样的事情,但它不起作用!

conn = fdb.services.connect(host='localhost', user='user', password='password', fb_library_name=API)
conn.restore(database.fbk, database.fdb)
restore_report = conn.readlines()

----> 1 conn = fdb.services.connect(host='localhost', user='user', password='password', fb_library_name=API) ----> 1 conn = fdb.services.connect(host='localhost', user='user', password='password', fb_library_name=API)
2 conn.restore(database.fbk, database.fdb) 2 conn.restore(database.fbk,database.fdb)
3 restore_report = con.readlines() 3 restore_report = con.readlines()
4 restore_report 4 恢复报告

TypeError: connect() got an unexpected keyword argument 'fb_library_name' TypeError:connect() 得到了一个意外的关键字参数“fb_library_name”

conn = fdb.services.connect(host='localhost', user='user', password='password')
conn.restore(database.fbk, database.fdb)
restore_report = conn.readlines()

DatabaseError: ('Services/isc_service_attach:\n- SQLCODE: -902\n- Unable to complete network request to host "localhost".\n- Failed to establish a connection.', -902, 335544721) DatabaseError: ('Services/isc_service_attach:\n- SQLCODE: -902\n- 无法完成对主机“localhost”的网络请求。\n- 无法建立连接。', -902, 335544721)

Assuming you want to use Firebird Embedded (judging by your previous question), the following works for me:假设您想使用 Firebird Embedded(根据您之前的问题判断),以下对我有用:

import fdb

fdb.load_api('C:/Program Files/Firebird/Firebird-3.0.5.33220-0_x64/fbclient.dll')

def report_progress(line):
    print(line)

svc = fdb.services.connect(user='sysdba', password='masterkey')
svc.restore('c:/db/somedatabase.fbk', 'c:/db/somedatabase.fdb', callback=report_progress)

That is, explicitly load the fbclient.dll associated with Firebird embedded (see also Firebird connection on a local database impossible within a Python script ), do not specify a hostname in the fdb.services.connect so it will use the Firebird Embedded service manager instead of trying to connect to the localhost Firebird Server (which isn't running in your case).也就是说,显式加载与 Firebird Embedded 相关联的fbclient.dll (另请参阅在 Python 脚本中不可能在本地数据库上连接 Firebird ),不要在fdb.services.connect中指定主机名,因此它将使用 Firebird Embedded 服务管理器而不是尝试连接到本地主机 Firebird 服务器(在您的情况下没有运行)。

Instead of the callback I use to report the progress, you can also use the readlines() or wait() methods.除了我用来报告进度的回调之外,您还可以使用readlines()wait()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM