MySQL Client

Table of Contents

  1. Packages
  2. Library
  3. License

Using this package you can execute SQL requests on a MySQL server directly from your mbed. It uses the Networking stack.

Packages

Precompiled version:

Library

Architecture

This MySQL client implements a limited subset of the MySQL internal client/server protocol (including authentication), for server versions 4.1 and newer.

Includes

#include "MySQLClient.h"

Reference

Import program

Public Member Functions

  MySQLClient ()
  Instantiates the MySQL client.
MySQLResult   open ( Host &host, const string &user, const string &password, const string &db, void(*pMethod)( MySQLResult ))
  Opens a connection to a server.
template<class T >
MySQLResult   open ( Host &host, const string &user, const string &password, const string &db, T *pItem, void(T::*pMethod)( MySQLResult ))
  Opens a connection to a server.
MySQLResult   sql (string &sqlCommand)
  Executes an SQL command.
MySQLResult   exit ()
  Closes the connection to the server.
void  setTimeout (int ms)
  Setups timeout.
virtual void  poll ()
  This method can be inherited so that it is called on each Net::poll() call.

Protected Member Functions

void  close ()
  This flags the service as to be destructed if owned by the pool.

Example

This example inserts a record into a table named 'Test' containing one text column named 'Test'. Configure the parameters at the top of the example accordingly to your setup.

#include "mbed.h"
#include "EthernetNetIf.h"
#include "MySQLClient.h"

#define SQL_SERVER   ""
#define SQL_USER     ""
#define SQL_PASSWORD ""
#define SQL_DB       ""

EthernetNetIf eth; 
MySQLClient sql;

MySQLResult sqlLastResult;
void onMySQLResult(MySQLResult r)
{
  sqlLastResult = r;
}

int main()
{
  printf("Start\n");

  printf("Setting up...\n");
  EthernetErr ethErr = eth.setup();
  if(ethErr)
  {
    printf("Error %d in setup.\n", ethErr);
    return -1;
  }
  printf("Setup OK\n");
  
  Host host(IpAddr(), 3306, SQL_SERVER);
  
  //Connect
  sqlLastResult = sql.open(host, SQL_USER, SQL_PASSWORD, SQL_DB, onMySQLResult);
  while(sqlLastResult == MYSQL_PROCESSING)
  {
    Net::poll();
  }
  if(sqlLastResult != MYSQL_OK)
  {
    printf("Error %d during connection\n", sqlLastResult);
  }
  
  //SQL Command
  //Make command
  char cmd[128] = {0};
  const char* msg="Hello World!";
  sprintf(cmd, "INSERT INTO Test (Test) VALUES('%s')", msg);
  
  //INSERT INTO DB
  string cmdStr = string(cmd); 
  sqlLastResult = sql.sql(cmdStr);
  while(sqlLastResult == MYSQL_PROCESSING)
  {
    Net::poll();
  }
  if(sqlLastResult != MYSQL_OK)
  {
    printf("Error %d during SQL Command\n", sqlLastResult);
  }
  
  sql.exit();
  
  while(1)
  {
  
  }
  
  return 0;
}

This program can be imported from here:

License