1

Firebird 3.0.4 is installed and Python v3.7.7 on a Windows10 64 bits system.

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!

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)
2 conn.restore(database.fbk, database.fdb)
3 restore_report = con.readlines()
4 restore_report

TypeError: connect() got an unexpected keyword argument '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)

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
hganger
  • 147
  • 2
  • 12
  • Are you using Firebird Embedded (as in your previous question), or do you actually have Firebird Server running on localhost (and if not, why are you specifying the `host` property)? Does it work if you call `fdb.load_api()` first? – Mark Rotteveel Jun 19 '20 at 21:15

1 Answers1

3

Assuming you want to use Firebird Embedded (judging by your previous question), the following works for me:

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).

Instead of the callback I use to report the progress, you can also use the readlines() or wait() methods.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 1
    Thank you again for your quick and working answer: very efficient! – hganger Jun 22 '20 at 06:26
  • Finally I was too quick to answer. The restore create a database with the same size but the content is modified; I don't get the same results with the same query using the DB restored with gbak and the DB restored with svc.restore. Any idea? I also tried svc.restore(databasebk, database) svc.wait() – hganger Jun 22 '20 at 14:16
  • @hganger In what way is the content modified? All this service call does is instruct the firebird embedded library to restore the database, then that uses what is basically the same code as the gbak executable to perform the actual restore. – Mark Rotteveel Jun 22 '20 at 14:43
  • I am really confused. Preparing the data to show you the modfied content I realize I was not using the right backup file. I stop coding today ... – hganger Jun 22 '20 at 17:19
  • @hganger No problem, we all have days like that. – Mark Rotteveel Jun 22 '20 at 17:23