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.
- 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
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.
To test out the environment, try executing the following. This should return the current version of the server (e.g. 126.96.36.1992)
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.
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.
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.
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.