RaspberryPI – project for „Baltic Robo Battles” 7

Now it is time to install ActianZen on RaspberryPI.

As everybody knows RaspberryPI has its own Operating System called Raspbian – it is clone of Debian, my favourite Linux version;) So in this case installation of ActianZen couldn’t be difficult and there were only few simple steps:

  1. download ActainZen for Raspbian from Actian www
  2. copy this file via SMB into RaspberryPI
  3. and make this little magic:
sudo cp Zen-Edge-Server-linux-armhf- /usr/local
cd /usr/local
$ sudo tar -zxf Zen-Edge-Server-linux-armhf-
$ cd actianzen/etc
$ sudo ./preinstall.sh
$ sudo ./postinstall.sh

Creating a Development Account and Working Folder
While the default pi user is good for many things, you will probably want to use a separate account for accesing DB. Create a user (we’ll use
bill in this example) that is in both the sudo and pvsw groups:

$ sudo adduser bill
$ sudo adduser bill sudo
$ sudo adduser bill pvsw

Now, log out of the pi user and log back into your newly-created user account.
To store all of your development files, we’re going to need a storage location to work from. This can be whatever and wherever you want it to be. In this example, we are going to create a folder inside the user’s home folder called MyPrograms, and we will refer to that folder from here on out. This command will create this folder for the user bill (and will work from any login):

$ cd
$ mkdir MyPrograms

The final step here is to configure this user’s environment, so that the database utilities and library files can be found when they are needed. We are going to simply concatenate the .bashrc file from the psql user (which was created by the Zen installer) to our current file:

$ cd
$ cat /home/psql/.bashrc >>.bashrc

You should log out and log back in one more time, so that your environment is properly configured.

And that’s it! Simple as piece of cake!

Installing SWIG with examples and docs

sudo apt-get update
sudo apt-get install swig
swig-doc swig-examples swig3.0-examples swig3.0-doc

Downloading and Installing the Btrieve 2 SDK
The next piece you need is the SDK download for Btrieve 2. If you have the SDK already exploded on
a Windows system, then you can simply copy the files from there. If not, the following steps will
explode the files directly on the Pi:
1) Go to https://esd.actian.com/product/Zen_PSQL in a web browser.

2) In the boxes provided, select SDKs and Btrieve 2.

3) Scroll down and open up the link for Btrieve 2, then click on the HTTP Download button for the Btrieve 2 Linux SDK for PSQL 13.

4) copy it via SMB to RaspberryPI to pi home folder and then:

tar xf PSQL-SDK-Btrieve2API-

6) This will explode the needed Btrieve 2 API components. Copy the following two files from the “swig” folder to your MyPrograms folder:

$ cd PSQL-SDK-Btrieve2API-
$ cp swig/btrievePython.swig ~/MyPrograms
$ cp swig/btrieveSwig.swig ~/MyPrograms

7) Finally, copy the following two files from the “include” folder to your working folder:

$ cp include/btrieveC.h ~/MyPrograms
$ cp include/btrieveCpp.h ~/MyPrograms

Building the SWIG Wrapper
Building the SWIG wrapper is the last setup step. Note that as the various components change, such as Python, SWIG, and the Btrieve 2 API, you may need to re-run this process to build a new SWIG wrapper from time to time.
1) Open a command prompt and change to your MyPrograms folder:

$ cd ~/MyPrograms

2) Build the SWIG Wrapper with this command:

$ sudo swig -c++ -D__linux__ -python btrievePython.swig

If this process is successful, you will have two new files in your current folder, namely btrievePython.py and btrievePython_wrap.cxx.

3) Use the command “vi setup.py” to create a new file and paste in the following text:

#!/usr/bin/env python
from distutils.core import setup, Extension
btrievePython_module = Extension('_btrievePython',
libraries=['btrieveCpp'] )
setup (name='btrievePython',
description="""Compile Btrieve 2 Python module""",
py_modules=["btrievePython"], )

4) Run this command to build the btrievePython module against Python3:

$ sudo python3 setup.py build_ext –inplace

You may see some warnings which can be ignored. When it is complete (about 30 seconds or so), you should now see the file _btrievePython.cpython-36m-arm-linux-gnueabihf.so in the
working folder. Note that your file and folder name will vary depending on the exact version of Python and the CPU you are running. For example, if you are running Python 3.5, then you will get the file _btrievePython.cpython-35m-arm-linux-gnueabihf.so instead.

5) Rename the newly-created file to _btrievePython.so with this command line.

$ mv _btrievePython.cpython-36m-arm-linux-gnueabihf.so _btrievePython.so


$ mv _btrievePython.cpython-35m-arm-linux-gnueabihf.so _btrievePython.so

Note that the files _btrievePython.so (generated in step 5) and btrievePython.py (generated in step 2) must be accessible by your Python application. Once you have followed these steps for a given CPU
and Python version, you can simply copy them around as needed.

Creating your Python Application
Once you’ve jumped through the above hoops, you can now import the newly-created module in order to access the Btrieve 2 API from with your code:

import sys
import btrievePython
  • The sample code in shows examples of several key operations, including:
  • Creating a New File
  • Opening an Existing File
  • Creating a New Index on an Existing File
  • Inserting Records
  • Reading All Records in Key Order
  • Performing a Record Lookup
import os
import sys
import struct
import btrievePython as btrv
btrieveFileName = "/usr/local/actianzen/data/DEMODATA/Test_Table.mkd"
recordFormat = "<iB32sBBBH"
recordLength = 42
keyFormat = "<i"
key1Format = "B32s"
Create a session:
btrieveClient = btrv.BtrieveClient(0x4232, 0) #B2
Specify FileAttributes for the new file:
btrieveFileAttributes = btrv.BtrieveFileAttributes()
rc = btrieveFileAttributes.SetFixedRecordLength(recordLength)
Specify Key 0 as an autoinc:
btrieveKeySegment = btrv.BtrieveKeySegment()
rc = btrieveKeySegment.SetField(0, 4, btrv.Btrieve.DATA_TYPE_AUTOINCREMENT)
btrieveIndexAttributes = btrv.BtrieveIndexAttributes()
rc = btrieveIndexAttributes.AddKeySegment(btrieveKeySegment)
rc = btrieveIndexAttributes.SetDuplicateMode(False)
rc = btrieveIndexAttributes.SetModifiable(True)
Create the file:
rc = btrieveClient.FileCreate(btrieveFileAttributes, btrieveIndexAttributes,
btrieveFileName, btrv.Btrieve.CREATE_MODE_OVERWRITE)
if (rc == btrv.Btrieve.STATUS_CODE_NO_ERROR):
print('File ', btrieveFileName, ' created successfully!')
print('File ', btrieveFileName, ' not created; ', rc, ': ', btrv.Btrieve.StatusCodeToString(rc))
Allocate a file object:
btrieveFile = btrv.BtrieveFile()
Open the file:
rc = btrieveClient.FileOpen(btrieveFile, btrieveFileName, None, btrv.Btrieve.OPEN_MODE_NORMAL)
if (rc == btrv.Btrieve.STATUS_CODE_NO_ERROR):
print('File open successful!')
print('File open failed - status: ', rc, ': ', btrv.Btrieve.StatusCodeToString(rc))
Create Key 1 as a String with Null Indicator Byte:
btrieveKey1aSegment = btrv.BtrieveKeySegment()
rc = btrieveKey1aSegment.SetField(4, 1, btrv.Btrieve.DATA_TYPE_NULL_INDICATOR_SEGMENT)
rc = btrieveKey1aSegment.SetDescendingSortOrder(True)
btrieveKey1bSegment = btrv.BtrieveKeySegment()
rc = btrieveKey1bSegment.SetField(5, 32, btrv.Btrieve.DATA_TYPE_CHAR)
btrieveIndex1Attributes = btrv.BtrieveIndexAttributes()
rc = btrieveIndex1Attributes.AddKeySegment(btrieveKey1aSegment)
rc = btrieveIndex1Attributes.AddKeySegment(btrieveKey1bSegment)
rc = btrieveIndex1Attributes.SetDuplicateMode(btrv.Btrieve.DUPLICATE_MODE_ALLOWED_NONREPEATING)
rc = btrieveIndex1Attributes.SetModifiable(True)
rc = btrieveFile.IndexCreate(btrieveIndex1Attributes)
if (rc == btrv.Btrieve.STATUS_CODE_NO_ERROR):
print('Index 1 created successfully!')
print('Index 1 not created; error: ', rc, ': ', btrv.Btrieve.StatusCodeToString(rc))
Insert records:
iinserting = True
while iinserting:
new_name = input('Insert name (Q to quit): ' )
if new_name.lower() == 'q':
iinserting = False
record = struct.pack(recordFormat, 0, 0, new_name.ljust(32).encode('UTF-8'), 0, 22, 1, 2018)
rc = btrieveFile.RecordCreate(record)
if (rc == btrv.Btrieve.STATUS_CODE_NO_ERROR):
print(' Insert successful!')
print(' Insert failed - status: ', rc, ': ', btrv.Btrieve.StatusCodeToString(rc))
Get Record count:
btrieveFileInfo = btrv.BtrieveFileInformation()
rc = btrv.BtrieveFile.GetInformation(btrieveFile, btrieveFileInfo)
print('\nTotal Records inserted =', btrieveFileInfo.GetRecordCount())
Display all records in sorted order
print('\nHere is a list of the names in alphabetical order:')
readLength = btrieveFile.RecordRetrieveFirst(1, record, 0)
while (readLength > 0):
recordUnpacked = struct.unpack(recordFormat, record)
print(' ID:', recordUnpacked[0], ' Name:', recordUnpacked[2].decode())
readLength = btrieveFile.RecordRetrieveNext(record, 0)
Look up record by name
ireading = True
while ireading:
find_name = input('\nFind name (Q to quit): ' )
if find_name.lower() == 'q':
ireading = False
key1Value = struct.pack(key1Format, 0, find_name.ljust(32).encode('UTF-8'))
readLength = btrieveFile.RecordRetrieve(btrv.Btrieve.COMPARISON_EQUAL, 1, key1Value, record)
if (readLength > 0) :
recordUnpacked = struct.unpack(recordFormat, record)
print(' Matching record found: ID:', recordUnpacked[0], ' Name:', recordUnpacked[2].decode())
print(' No record found matching "'+find_name+'"')
status = btrieveFile.GetLastStatusCode()
if (status != 4):
print(' Read error: ', status, ': ', btrv.Btrieve.StatusCodeToString(status))
Close the file:
rc = btrieveClient.FileClose(btrieveFile)
if (rc == btrv.Btrieve.STATUS_CODE_NO_ERROR):
print('File closed successfully!')
print('File close failed - status: ', rc, ': ', btrv.Btrieve.StatusCodeToString(status))

Running Your Python Application
Running the application fairly easy. Simply launch Python and pass in the script name!

$ python3 test_btr2.py

An now the question is:

How to connect to ActianZen DB located on Windows server from RaspberryPI?…

2 myśli na “RaspberryPI – project for „Baltic Robo Battles” 7

  1. It’s a shame you don’t have a donate button! I’d without
    a doubt donate to this outstanding blog!
    I suppose for now i’ll settle for book-marking and adding your
    RSS feed to my Google account. I look forward to new updates and will talk
    about this website with my Facebook group. Talk soon!

  2. Great post. I used to be checking constantly this weblog and I’m impressed! Very useful information particularly the closing phase 🙂 I handle such info much. I was seeking this certain information for a very lengthy time. Thank you and best of luck.

Dodaj komentarz

Twój adres e-mail nie zostanie opublikowany.