facebooklinkedinrsstwitterBlogAsset 1PRDatasheetDatasheetAsset 1DownloadForumGuideLinkWebinarPRPresentationRoad MapVideofacebooklinkedinrsstwitterBlogAsset 1PRDatasheetDatasheetAsset 1DownloadForumGuideLinkWebinarPRPresentationRoad MapVideo

Btrieve2 SDK Setup for Python

In this tutorial, we will install and set up the Btrieve2 SDK for Actian Zen.

This topic includes the following steps:

  • What is Btrieve2?
  • Btrieve2 Extension Files for Python
  • Creating Python Extension Files on Windows
  • Creating Python Extension Files on Linux

What is Btrieve2?

The Btrieve2 SDK offers a simplified, more intuitive, object-oriented application programming interface (API) covering the full set of Btrieve calls to the Actian Zen database engine.

The advantages of using the Btrieve2 SDK for application development with Actian Zen include:

  • Native language syntax for working with the database
  • NoSQL-style database access paradigms
  • Easy database integration into your application in a variety of languages

Btrieve2 Extension Files for Python

The Btrieve2 SDK is provided for C/C++ developers, but we also supply SWIG (Simplified Wrapper and Interface Generator) interface files to create extension files so the API can be used by Python, Perl, and PHP developers.

In this tutorial we’ll walk through the process for using the SWIG interface files to create Python extensions for development on Windows and Linux.

Creating Python Extension Files on Windows

It takes just a few steps to set up Python extension files on Windows for the Btrieve2 SDK using the SWIG interface files. We assume you’ve already installed the Actian Zen database engine and Python for Windows as discussed in another tutorial.

Download and unzip the latest release of SWIG for Windows. This process has been tested with both versions 3.0.12 and 4.0.0. For simplicity, make sure SWIG is installed in a location available on your Windows Path environment variable, or remember the directory in which you installed it when you are later running the application.

Download and unzip Btrieve2 Windows SDK from the Actian Electronic Software Distribution page. Download the Btrieve2 SDK version that’s the same as your installed Actian Zen version (for example, v14). Downloads require an Actian ID, so make sure to log in or register an Actian ID before starting the download.

You can also select the package to download by choosing the following in the menus:

  • Product: Actian Zen
  • Release: SDKs
  • Platform: Btrieve 2
  • And then select Btrieve 2 | Release.

Visual Studio 2015 or later is also required.

Create a folder for your Python code. We’ll use \MyPrograms in the examples here.

Extract the Btrieve2 SDK and copy the following files from there to the MyPrograms folder:

  • \win64\btrieveCpp.lib
  • \win64\btrieveC.lib
  • \include\btrieveC.h
  • \include\btrieveCpp.h
  • \swig\btrievePython.swig
  • \swig\btrieveSwig.swig

Open a command prompt and change to the MyPrograms folder, then enter the following command:

swig -c++ -D_WIN64 -python btrievePython.swig

This creates the following files:

  • btrievePython.py
  • btrievePython_wrap.cxx

Create a text file in MyPrograms called setup.py containing the following:

from distutils.core import setup, Extension
btrievePython_module = Extension('_btrievePython', 
  sources=['btrievePython_wrap.cxx'],
  library_dirs=['<MyPrograms location>'], 
  libraries=['btrieveCpp'],  )
setup (name='btrievePython',
  version='1.0',
  author='Actian',
  description="""Compile Btrieve 2 Python module""",
  ext_modules=[btrievePython_module],
  py_modules=["btrievePython"], )

Build the btrievePython module by running the following command at the prompt:

python setup.py build_ext --plat-name="win-amd64"

This creates the compiled Python file build\lib.win-amd64-3.6\_btrievePython.cp36-win_amd64.pyd.

Rename the .pyd file to just _btrievePython.pyd and copy it to MyPrograms or the DLL directory in your Python installation.

Once these steps are completed, you should be able to execute “import btrievePython” in a Python environment or program, and then you are ready to write Btrieve2 applications in Python.

Creating Python Extension Files on Linux

Setting up Python extension files on Linux for the Btrieve2 API using the SWIG interface files takes just a few steps.

We assume you’ve already installed the Actian Zen database engine and Python (along with additional required packages) as discussed in another tutorial.

Create a folder for your Python code. We’ll use /project in the examples here.

mkdir ~/project && cd ~/project

Download and untar the latest release of SWIG for Linux, or use your distro’s package manager to install the swig package.

sudo apt-get update && sudo apt-get install swig

Download Btrieve2 Linux SDK from the Actian Electronic Software Distribution page. Download the Btrieve2 SDK version that’s the same as your installed Actian Zen version (for example, v14). Downloads require an Actian ID, so make sure to log in or register an Actian ID before starting the download.

You can also select the package to download by choosing the following in the menus:

  • Product: Actian Zen
  • Release: SDKs
  • Platform: Btrieve 2
  • And then select Btrieve 2 | Release.

Untar the Btrieve2 SDK into the current /project directory. Replace [filename] with the name of your downloaded file.

tar -zxf [filename].tar.gz

Enter the SDK directory and copy the required files to your /project directory. Replace [directory] with the name of the directory for your extracted installation.

cd [directory]
cp swig/btrievePython.swig ~/project/
cp swig/btrieveSwig.swig ~/project/
cp include/btrieveC.h ~/project/
cp include/btrieveCpp.h ~/project/

Change back to your project directory and run SWIG to create a wrapper:

cd ~/project
sudo swig -c++ -D__linux__ -python btrievePython.swig

Create a setup.py file in your /project directory containing the following:

#!/usr/bin/env python
from distutils.core import setup, Extension
btrievePython_module = Extension('_btrievePython',
  sources=['btrievePython_wrap.cxx'],
  library_dirs=['/usr/local/actianzen/lib64/'],
  runtime_library_dirs=['/usr/local/actianzen/lib64'],
  libraries=['btrieveCpp'])
setup(name='btrievePython',
  version='1.0',
  author='Actian',
  description="""Compile Btrieve 2 Python module""",
  ext_modules=[btrievePython_module],
  py_modules=["btrievePython"],)

Run the setup script to create a shared extension file:

sudo python3 setup.py build_ext --inplace

If you experience any errors here, make sure you installed all of the necessary dependencies covered in the “Linux and Python” topic under Actian Zen and Python.

Rename your newly created .so file to make it more usable:

mv _btrievePython.cpython-37m-x86_64-linux-gnu.so _btrievePython.so

Once these steps are completed, you should be able to execute “import btrievePython” in a Python environment or program, and then you are ready to write Btrieve 2 applications in Python.

If your execution of Python commands against the database returns status code 94, this indicates that the user does not have permission to create data files in the specified folder. You can correct this by changing the access rules for the project directory.

chmod 777 ~/project