Server Hosting Secrets
January 5th in Programming, Tutorials by Claudiu Popescu .

Mysql C API Programming

In this tutorial I will explain how to write a simple C++ program using MySQL C API.
This program is only a example in order to get you started in the right direction using C++ and MySQL.
If you’re planning to build the next rival to www.partypoker.com then you might need a little bit more training, but this should get you started.

Let’s start by including the required libraries:

#include <cstdlib>
#include <string.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

The next includes are for MySQL C API.

#include "mysql/my_global.h"
#include "mysql/mysql.h"

And the actual programming starts here. Simple MySQL query function:

int mysql_exec_sql(MYSQL *mysql,const char *create_definition)
{
 return mysql_real_query(mysql,create_definition,strlen(create_definition));
}

We need to declare a few variables before we can use them.
You can replace (“code”) with your own file name, this program will read line by line from the file named code placed in the same directory.

int main(int argc, char **argv)
{
 string line;
 ifstream myfile ("code");
 MYSQL mysql;
 char record[1000];
 char query[1000],*end;

This is where we connect to the MySQL server, replace HOST with your server’s IP address, also replace USER and PASSWORD.

if(mysql_init(&mysql)==NULL)
 {
 printf("\nFailed to initate MySQL connection");
 exit(1);
 }
if (!mysql_real_connect(&mysql,"HOST","USER","PASSWORD",NULL,0,NULL,0))
{
 printf( "Failed to connect to MySQL: Error: %s\n", mysql_error(&mysql));
 exit(1);
}

In this section of code we will select a database and print if it was selected or if we encountered a error. Replace DATABASE with your own database.

if(mysql_select_db(&mysql,"DATABASE" )==0)
 printf( "Database Selected\n");
else
 printf( "Failed to connect to Database: Error: %s\n", mysql_error(&mysql));

The next section is a loop, first we open the file declared earlier, if the file is opened then the loop will begin. What will this loop do? It will read line by line the file opened and it will insert into MySQL a record for every line, the records will be inserted in the table called TABLE (replace it with your own table name).

 if (myfile.is_open())
 {
 while (! myfile.eof() )
 {
 getline (myfile,line);
 sprintf(record,"INSERT INTO TABLE VALUES ('%s')", line.c_str());
 if(mysql_exec_sql(&mysql,record)==0)
 printf( "Record Added\n");
 else
 printf( "Failed to add records: Error: %s\n", mysql_error(&mysql));
 }
 myfile.close();
 }

If the file can’t be opened the program will print this message.

 else cout << "Unable to open file";

Finally we close the MySQL connection.

mysql_close(&mysql);
}

After you put together the code it is time to compile it, here’s how:


g++ -o filename $(mysql_config --cflags) filename.cpp $(mysql_config --libs)

You can download the full source code by using the following link: MySQL C API (40)

VN:F [1.9.3_1094]
Rating: 10.0/10 (2 votes cast)
Mysql C API Programming, 10.0 out of 10 based on 2 ratings
The Short URL of this blog posting is http://tyn.li/5
Twitter this blog posting

Leave A Comment.