Home Contacts Terms Send money Cookie policy


Personal blogs


Acme's Friends

We would like to inform our clients that our shipping office will be closed from Aug 8 to Aug 23
PLEASE NOTE: This article is obsolete or related to a discontinued product.

How to compile MySQL C client API

This tutorial shows how to compile the MySQL 5.0.21 C client API for the FOX Board

MySQL (http://www.mysql.org) is one of the most used and faster SQL server in the world.
MySQL C client API ( http://dev.mysql.com/doc/refman/5.0/en/c.html ) are a set of functions in C that enable your C applications to make SQL queries to a remote MySQL Server across a TCP/IP connection.

Using the MySQL C client API on the FOX Board it is possible to build a powerful gateway between external devices like lights, switches, display, etc. and the values stored in a SQL database.

An example is to generate an SQL "INSERT" every time a switch is pressed or show the results of a complex SQL "SELECT" directly on a LCD display to see the live values of total sales amounts from your ecommerce site.

How to compile step by step the MySQL C API on the FOX SDK

Example 1 - An event logger

In this example we'll generate on the MySQL server one record each time we push an external switch. The record will have a timestamp to know when the switch was pressed.

A led will indicate if the new log record has generated on the MySQL server with a single blink. More blinks indicate an error condition.

Build the hardware

The external switch is connected to the FOX Board as shown on the following schematic diagram:

Schematic to connect an external switch

Schematic to connect an external LED

Create database and table on the MySQL server

Create a new database named logger on your MySQL server and a table named switch inside it. Create inside the table switch four fields: id, time, switchid and description.

To create all of them you can use directly this SQL commands file:

`switchid` VARCHAR( 20 ) NOT NULL ,
`description` VARCHAR( 50 ) NOT NULL ,
PRIMARY KEY ( `id` )

Be shure that your MySQL server is enabled to receive external transactions. To do that modify this line on /etc&msql/my.cnf:

Be shure also that you server can accept connection on default MySQL port 3306.

This is the source of logger.c:

#include "stdio.h"
#include "stdlib.h"
#include "unistd.h"    
#include "sys/ioctl.h"
#include "fcntl.h"     
#include "asm/etraxgpio.h"
#include "include/mysql.h" 

#define def_host_name   "" // Put here the IP address of your MySQL server
#define def_user_name   "fox"           // Put here your MySQL user
#define def_password    "fox"           // Put here your MySQL password
#define def_db_name     "logger"

MYSQL  *conn;
int fd;

void led_on() {
  int iomask;

void led_off() {
  int iomask;

void led_blink(int number) {
  int i;
  for (i=0;i<number;i++) 

int get_switch() { 
  int iomask;
  int value;


  if ((value&iomask)==0) return 1;
  else return 0;

int main (int argc, char *argv[])
  if ((fd = open("/dev/gpiog", O_RDWR))<0) {
    printf("Open error on /dev/gpiog\n");
  conn = mysql_init (NULL);
  if (conn == NULL)
   fprintf (stderr, "mysql_init() failed (probably out of memory)\n");
   exit (1);
  if (mysql_real_connect (
    conn,          /* pointer to connection handler */
    def_host_name, /* host to connect to */
    def_user_name, /* user name */
    def_password,  /* password */
    def_db_name,   /* database to use */
    0,             /* port (use default) */
    NULL,          /* socket (use default) */
    0)             /* flags (none) */
    == NULL)
    fprintf (stderr, "mysql_real_connect() failed:\n");
    fprintf (stderr, "Error %u (%s)\n", mysql_errno (conn), mysql_error (conn));
    exit (1);

  while (1) {
    if (get_switch()) {
      if (mysql_query (conn, "INSERT INTO switch (switchid,description) VALUES ('IOG16','Switch pressed')") != 0)
        printf("INSERT statement failed\n");
        exit (1);
        printf ("INSERT statement succeeded: %lu rows affected\n",(unsigned long) mysql_affected_rows (conn));

  mysql_close (conn);
  exit (0);

This is the result thet we obtain in the switch table:

mysql> select * from switch;
| id | time           | switchid | description    |
|  1 | 20060530074807 | IOG16    | Switch pressed |
|  2 | 20060530074809 | IOG16    | Switch pressed |
|  3 | 20060530074811 | IOG16    | Switch pressed |
|  4 | 20060530074812 | IOG16    | Switch pressed |
|  5 | 20060530074813 | IOG16    | Switch pressed |
|  6 | 20060530074813 | IOG16    | Switch pressed |
|  7 | 20060530074814 | IOG16    | Switch pressed |
|  8 | 20060530074815 | IOG16    | Switch pressed |
|  9 | 20060530074815 | IOG16    | Switch pressed |
9 rows in set (0.00 sec)

Related links

Acme Systems srl
Via Aldo Moro 53 - 00055 Ladispoli (RM) - Italy
P.IVA/C.F. 08114831004
Tel + - Fax +39.06.622.765.31
PEC: acmesystemssrl@pec.it
Iscritta al Registro delle Imprese di Roma al n. 08114831004

Atmel© Certified Partner

Acme Systems provides this documentation "as is" without warranty or guarantees of any kind. We not provide any direct support for the Open Source preinstalled software but, through these pages and forum posts, all of the information required to obtain the sources, install, use and update the Open Source software that can be run on the Acme boards. Please note that all of the preinstalled software, used on the Acme Systems products, are Open Source so you have to check the license terms provided by the authors (usually the GPL) before using it in any commercial or non-commercial product, by yourself. Before to contact us please note that WE ARE MAINLY HARDWARE DESIGNERS and NOT LINUX GURUS and therefore could be better to post your questions to the Acme Systems forum and ensure that all of the site contributors and large software community can read and reply to your questions.

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.