You are viewing an older revision! See the latest version

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: http://mbed.org/users/donatien/programs/MySQLClient

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

MySQLClient()

Instantiate the MySQL client.

Requests

MySQLResult open(Host& host, const string& user, const string& password, const string& db, void (*pMethod)(MySQLResult))
MySQLResult open(Host& host, const string& user, const string& password, const string& db, T* pItem, void (T::*pMethod)(MySQLResult))

Opens a connection to the server host using username user, password passowrd and selecting database db. On completion of this call (and any further one), the callback set in parameter is fired with the result of that command in parameter.

MySQLResult sql(string& sqlCommand)

Executes an SQL request (sqlCommand) on the SQL server. This is a non-blocking function. On completion, the callback set in the open function is fired with the result of the command in parameter.

MySQLResult exit()

Closes the connection to the server.

Parameters

void setTimeout(int ms)

Sets the time in milliseconds before a request should be considered as having timed out.

Result codes

enum MySQLResult
{
  MYSQL_OK,
  MYSQL_PROCESSING,
  MYSQL_PRTCL,
  MYSQL_SETUP, //Not properly configured
  MYSQL_DNS, //Could not resolve name
  MYSQL_AUTHFAILED, //Auth failure
  MYSQL_READY, //Ready to send commands
  MYSQL_SQL, //SQL Error
  MYSQL_TIMEOUT, //Connection timeout
  MYSQL_CONN //Connection error
};

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: http://mbed.org/users/donatien/programs/MySQLClientExample.

License


All wikipages