Skip to Content

In the last couple of years there has been an explosion of small, low-power, ARM-based computing devices that have hit the market. One example of these is the incredibly affordable Raspberry Pi. These ARM-based devices are excellent for embedded applications that run at the edge of a network.

This trend was very interesting for us because SQL Anywhere is a database which is designed to run in embedded application at the edge of a network. Sounds like a good fit, doesn’t it?

Well, we thought so too! That is why I am happy to announce today that SAP SQL Anywhere 16 is now available on Linux ARM

If you want to test it out, go grab yourself a Raspberry Pi board for ~$35 and follow the steps below.

Pre-Requisites

  • Raspberry Pi installed with Raspbian (other Linux distributions and other ARMv6 and ARMv7 devices may work as well, but some commands may be different)
  • Internet connection to Raspberry Pi
  • Shell access to Raspberry Pi (either through SSH or connected display)

Getting Started with SQL Anywhere on Raspberry Pi

Register for the latest SAP SQL Anywhere 16 Developer Edition: https://global.sap.com/campaign/ne/sybase/sql_anywhere_16_download_program/index.epx?kNtBzmUK9zU

After registering you will be sent a registration key over email.


Open a shell on your Raspberry Pi (either through SSH or from the desktop). Download and extract the SQL Anywhere Developer Edition.


cd /tmp
wget http://d5d4ifzqzkhwt.cloudfront.net/sqla16developer/bin/sqla16forlinuxarm.tar.gz
tar -xvf sqla16forlinuxarm.tar.gz










Install SQL Anywhere using the key your received earlier over email (accept all of the defaults)


cd ga1600
sudo ./setup










Return to your home directory


cd ~










The SQL Anywhere executable and libraries are not added to the PATH and LD_LIBRARY_PATH environment variables automatically. You can add this to the current shell’s environment by sourcing the configuration files.


. /opt/sqlanywhere16/bin32/sa_config.sh










To test out the environment, try executing the following. This should return the current version of the server (e.g. 16.0.0.1972)


dbsrv16 -v










Now that everything is setup, it it time to create a small application. Create a directory to store your application.



mkdir hellosensor
cd hellosensor









Next, we need to initialize an empty database. We will call this database hellosensor.db.


dbinit hellosensor.db









Start the database server (the -ud switch starts the server as a background daemon).


dbsrv16 -ud hellosensor.db










Python is the preferred development language for the Raspberry Pi, and it comes preinstalled, so that is what we will use. In order to connect to SQL Anywhere, we will need to install the SQL Anywhere Python driver. This can be installed through the Python Package Manger (pip).

First, make sure pip is installed.


sudo apt-get install python-pip









Then, install the SQL Anywhere Python Driver through pip.


sudo pip install sqlanydb









Create a file called helloworld.py with the following contents.


import sqlanydb
conn = sqlanydb.connect(uid='dba', pwd='sql', eng='hellosensor', dbn='hellosensor' )
curs = conn.cursor()
curs.execute("select 'Hello, world!'")
print "SQL Anywhere says: %s" % curs.fetchone()
curs.close()
conn.close()









Save the file, and test it out.


python helloworld.py










If successful, you should see this message.


SQL Anywhere says: Hello, world!










At this point, everything should be set up correctly. Let’s create another application that reads fictitious sensor readings and stores them in a table. Create a file called hellosensor.py with the following contents.


# Import the SQL Anywhere Python driver
import sqlanydb
from time import sleep
from random import random
# Connect to the database
conn = sqlanydb.connect(uid='dba', pwd='sql', eng='hellosensor', dbn='hellosensor' )
# Create the table to hold the sensor readings (if not exists)
def setup():
  curs = conn.cursor()
  sql = ("CREATE TABLE IF NOT EXISTS Sensor("
      "  id INTEGER PRIMARY KEY DEFAULT AUTOINCREMENT,"
      "  reading FLOAT NOT NULL,"
      "  timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL"
      ")")
  curs.execute(sql)
  curs.close()
# This function would normally read a real sensor (temperture, etc)
# For this sample, it returns a random float between 0 and 100
def read_sensor():
  return random() * 100
# Write the sensor value into the database
def write_reading(value):
  curs = conn.cursor()
  sql = "INSERT INTO Sensor(reading) VALUES (?)"
  curs.execute(sql, (value,))
  curs.close()
  # IMPORTANT! SQL Anywhere does not commit by default
  # An explicit commit is required.
  conn.commit()
# Create tables
setup()
print('Press Ctrl-C to stop...')
# Read sensor every 3 seconds, and insert into database
# Run until Ctrl-C is pressed
try:
  while True:
    value = read_sensor()
    print("Current sensor reading is %s" % (value,))
    write_reading(value)
    sleep(3)
except KeyboardInterrupt:
  # Close the connection
  conn.close()










(In a real application, you would probably replace read_sensor to do something with GPIO pins and the physical world such as logging a temperature sensor)

Save the file, and test it out.


python hellosensor.py









The output should look similar to this.


Press Ctrl-C to stop...
Current sensor reading is 67.012247981
Current sensor reading is 83.2578335957
Current sensor reading is 71.2944099229
Current sensor reading is 88.3533857105
Current sensor reading is 99.646246581









Everything is working, and you are successfully logging data to an embedded SQL Anywhere database. In the next blog post we will connect with the graphical administration tools from your development machine to view the saved data.

To report this post you need to login first.

10 Comments

You must be Logged on to comment or reply to a post.

  1. John Astill

    Hi Eric,

    Great blog thanks, and a super cool tool.

    One small thing, I had to do the following to get the python script to run:

    export PYTHONPATH=$PYTHONPATH:/opt/sqlanywhere16/sdk/python .


    The script

    1. . /opt/sqlanywhere16/bin32/sa_config.sh  

    appears to set the LD_LIBRARY path.


    Regards

    JohnA


    (0) 
      1. Michael Will

        Hi Eric,

        I was very happy to see your how-to, both as a current Raspberry Pi enthusiast and a one-time Sybase aficionado. I tried using a Pi2 and got as far as the dbsrv16 -v test, but it was all non-functional at that point. I know that binary compatibility from Pi1 to Pi2 is not 100% (there are two separate distros of Raspian). It didn’t even occur to me until I finished installing that the SQLA distro may be ARM6-specific (Pi2 is ARM7).

        (0) 
        1. Eric Farrar Post author

          Thanks, Michael.

          I would be interested for more details on the problems you ran into when running the server. As you say, the new Pi it is a different version of ARM. However, our distribution supports both ARM6 and ARM7 (this was noted in the prerequisites above). We have been running it on the new Raspberry Pi boards, as well as on other ARM7 boards (e.g. Olimex A20) without issue.

          Can you provide more details please?

          Thanks,

          (0) 
          1. Michael Will

            Eric,

            ./setup ran fine and installed everything, but I just get “command not found” when I try to run anything (eg dbsrv16 -v), even though I can see it, it’s executable, and I have permission. The last time I saw this behaviour I was trying to run 32-bit binaries on a 64-bit PC system, but I don’t think ARM will be 64-bit until ARM8.

            Anyway, I’ll figure it out when I have time to play with it. It’s very possible that I have something misconfigured on that Pi2 (I’m not familiar at all with it yet).I certainly appreciate your tutorial, and I don’t want you to spend your time on this.

            Cheers

            (0) 
            1. Tyson Lewis

              If you installed it to the default directory, you’ll have to source the configuration file to get the environment variables set up.

              source /opt/sqlanywhere16/bin32/sa_config.sh

              Note that if you do not include the “source” keyword/command, the environment variables won’t persist after the script is ran.

              You can also confirm the variables are set up correctly by looking at the environment variables:

              env | grep PATH

              That command should show /opt/sqlanywhere16/bin32 in the PATH variable and /opt/sqlanywhere16/lib32 in the LD_LIBRARY_PATH variable.

              Hope this helps!

              (0) 
              1. Michael Will

                Yup, that was it. I had omitted the ‘source’ command, which threw a permissions error, and never updated the environment. Thanks so much Tyson.

                I got helloworld.py to work, so it’s full steam ahead. Thanks again Eric.

                (0) 
  2. P. Lans

    Hi Eric,

    Thanks for this great blog.


    Installing SQLAnywhere and the python module on the Raspberry Pi turned out to be very easy.

    Persisting the environment variables was a little harder. Pi simply refused to, so I ended up editing the bashrc file manually.

    Still don’t understand why the python script works from the command line, and Idle gives an error on import of the sqlanydb module, but I’ll figure that out some other time.

    Now on to the second demo, then to incorporating it with java.

    Best regards,

    Peter

    (0) 

Leave a Reply