Setup MySQL C Access

Installing The MySQL C libraries

MySQL comes with C-libraries:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install libmysqlclient-dev

#include <sys/time.h>
#include <mysql.h>
#include <iostream>
using namespace std;

MYSQL *connection,mysql;
MYSQL_RES *result;
MYSQL_ROW row;
int query_state;

int main()
{
 mysql_init(&mysql);
 connection = mysql_real_connect(&mysql,"hostName","userName","password","dbName",0,0,0);
 if(connection==NULL)
 {
  cout<<mysql_error(&mysql)<<endl;
 }
 else
 {
  cout<<"CONNECTED!"<<endl;
 }
if (mysql_query(connection, "INSERT INTO envTemperature VALUES('','2','60','2014-02-08','17:12:01')")) {
      cout<<"\n not success!\n";
  }
  mysql_close(connection);
 return 0;
}

Save it in a directory on the RPI, then on the command line go to that directory and use this to compile it:

g++ main.c `mysql_config --cflags` `mysql_config --libs`

No comments:

Post a Comment