Recent changes
Slingshot user guide
tag Guide, user
NFCLamp user guide
tag Guide, user
Homepage
MPL115A2
Compiler Error 42
From the mbed microcontroller Cookbook.  

MySQL Client

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

Packages

Precompiled version:

» Import this programMySQLClient

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

Code

#include "MySQLClient.h"

Reference

» Import this 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.

Code

#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




calendar Page history
Last modified 28 Jul 2010, by   user Donatien Garnier   tag No tags | 6 comments  

6 comments on MySQL Client:

01 Feb 2011

Any examples on retrieving rows on a SELECT statement? I've worked with MySQL before and would use:

while(MySQL.FetchNext()) { ulPermissions = MySQL.Field("AUTH").asLong(); }

07 Aug 2011

I am also highly interested for Select statement. Any progress for Select?

15 Sep 2011

how to get the host's ip_addr?????

21 Jan 2012

I got everytime 2 failures...

Error 2 during connection and Error 3 during SQL Command.

I don't know why and what does this mean? I use MySQL Server 5.1

20 Feb 2012

Important:

Host host(IpAddr(), 3306, SQL_SERVER);

In this line what is meant by IpAddr() ? I have to give the Ip address of the host? Or just keep that line unchanged?

16 Mar 2012

Hi,

After converting the Code to C I found some problems in the Send Auth:

Code

if( m_len - (m_pPos - m_buf) != 44)
{
   //We only support protocol >= mysql-4.1
    DBG("Message after pcsz has wrong len (%d != 44)...\n", m_len - (m_pPos - m_buf));
    onResult(MYSQL_PRTCL);
    return;
  }

Should be changed to:

Code

        uint16_t serverFlags = *((uint16_t*)&m_pPos[13]);

	if( ! (serverFlags & (CLIENT_RESERVED | CLIENT_PROTOCOL_41	| CLIENT_SECURE_CONNECTION  )) )   //  New 4.1 protocol
	{
		//We only support protocol >= mysql-4.1
		DEBUGF(LOG,("We only support protocol >= mysql-4.1\n"));
		onResult(MYSQL_PRTCL);
                return;
	}

A server might send server capabilities of : 0xf7ff and not 0x2c82 as expected in the code.

Regards, Nick

Please login to post comments.