InterBase and Python

Gesendet durch auf In Data

  

This blog post will cover basic usage of InterBase and Python using Eclipse.

  1. Install PyDev using the Eclipse IDE
  2. Install Python3.4
  3. Configure Eclipse to use Python
  4. Download the FDB 1.4.9 driver sources
  5. Edit the sources to use the InterBase client library gds32.dll instead of the default FireBird Client library
  6. Install the driver to your local Python Environment
  7. Write a simple test to connect

 

 

Install PyDev using the Eclipse IDE

The instructions for installing PyDev are here:
http://www.pydev.org/manual_101_install.html

In the Eclipse IDE I chose Help > Install Updates and Entered the URL for PyDev

1


Install Python3.4

I chose the Windows x86 .msi installer from here
https://www.python.org/downloads/release/python-342/ and installed to C:\Python34_2

Configure Eclipse to use Python

In Eclipse select Windows>Preferences

Under the Pydev tree, select PyDev> Interpreters> Python Interpreter

Click New and set the Python Interpreter name and executable to the previously install Python

2

 

 

Download the FDB 1.4.9 driver sources

The sources are available here
https://pypi.python.org/pypi/fdb

I extracted the sources to
C:\fdb-1.4.9

Edit the sources to use the InterBase client library gds32.dll instead of the FireBird default library

There were 2 files that needed to be changed to work with Embarcadero InterBase XE7 instead of the Default FireBrid server.

Ibase.py
Starting at line 1163

Original

fb_library_name = find_library('fbclient.dll')
    if not fb_library_name:
        # let's try windows registry
        if PYTHON_MAJOR_VER == 3:
            import winreg
        else:
            import _winreg as winreg

        # try find via installed Firebird server
        baseKey = 'SOFTWARE\Firebird Project\Firebird Server\Instances'
        key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, baseKey)
        instFold = winreg.QueryValueEx(key,'DefaultInstance')
        fb_library_name = os.path.join(os.path.join(instFold[0], 'bin'), 'fbclient.dll')

New

fb_library_name = find_library('gds32.dll')
if not fb_library_name:
    # let's try windows registry
    if PYTHON_MAJOR_VER == 3:
        import winreg
    else:
        import _winreg as winreg

    # try find via installed Firebird server
    baseKey = 'SOFTWARE\Embarcadero\InterBase\Servers' 
    key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, baseKey)
    instFold = winreg.QueryValueEx(key,'RootDirectory')
    fb_library_name = os.path.join(os.path.join(instFold[0], 'bin'), 'gds32.dll')

 

Starting at line 1479

Original

self.fb_interpret = fb_library.fb_interpret
self.fb_interpret.restype = ISC_LONG
self.fb_interpret.argtypes = [STRING, c_uint, POINTER(POINTER(ISC_STATUS))]

New

#self.fb_interpret = fb_library.fb_interpret
#self.fb_interpret.restype = ISC_LONG
#self.fb_interpret.argtypes = [STRING, c_uint, POINTER(POINTER(ISC_STATUS))]

 

Fbcore.py

Starting at line 542

Original

result = api.fb_interpret(msg, 512, pvector)

New

result = api.isc_interprete(msg, pvector)           

 

Starting at line 923

Original

verstr = self.db_info([isc_info_firebird_version])[isc_info_firebird_version]   

New

verstr = self.db_info([isc_info_version])[isc_info_version]

 

Starting at 1343

Original

elif infoCode in (isc_info_version, isc_info_firebird_version):   

New

elif infoCode in (isc_info_version, isc_info_version):

 

Install the driver to your local Python Environment

After making the changes to ibase.py and fbcore.py, you are now ready to install the driver to your Python Environment.

From the commandline navigate to the fdb1.4.9 directory.
C:\fdb-1.4.9>python.exe setup.py install

After installed you should see the message
Installed c:\python34_2\lib\site-packages\fdb-1.4.9-py3.4.egg
Processing dependencies for fdb==1.4.9
Finished processing dependencies for fdb==1.4.9

Write a simple test to connect to InterBase in Eclipse

In Eclipse select file > new > project > PyDev > PyDev project

3

4

More complete instructions can be found here
http://www.pydev.org/manual_101_project_conf.html

After creating a blank Python module, I added this code.

# The server is named 'localhost'; the database file is at 'C:\ProgramData\Embarcadero\InterBase\gds_db\examples\database\employee.gdb'.
con = fdb.connect(dsn='localhost:C:\ProgramData\Embarcadero\InterBase\gds_db\examples\database\employee.gdb', user='sysdba', password='masterkey')

cur = con.cursor()

# Execute the SELECT statement:
cur.execute("select * from country")

# Retrieve all rows as a sequence and print that sequence:
print (cur.fetchall())

 

This is all the code needed to import the driver, create a connection, create a cursor, select the data, and print the result.

After adding this code to a blank Python Module, select the Module > Run As > Python Run.

5

 



Comments