You are on page 1of 6

Menu

Softexpert Moldova – A Blog for IT Professionals


for IT Professionals

How to connect to a MySQL DB using c++ Linux?


Posted by Vladimir Iachimovici

C++ is a very good programming language with high-level and low-level capabilities.

Linux is a Unix-like computer operating system. It promotes community of free and open source
software. Almost using those instruments it is possible to make a complex and sophisticate
enterprise system.

MySQL is a multithreaded, multi-user SQL database management system. It is a popular


database system, which has more than 10 million installations.

In this article is shown how to connect to a MySQL database using C++ under a Linux operating
system.

First of all we include all needed libraries:

#include <sys/time.h>

#include <stdio.h>

#include <mysql.h>

Now we declare main function:

int main(char **args)

// code

return 0;

};

Next, we declare all necessary variables:

MYSQL_RES *result;

MYSQL_ROW row;

MYSQL *connection, mysql;

int state;

Of course if we want to use MySQL database, we must connect to it, using the next code:
mysql_init(&mysql);

connection = mysql_real_connect(&mysql,host,usr,pswd,database,0,0,0);

Where

host – is a host name, the location of the MySQL database server, for example “localhost” or
“remotehost.com”

usr – is the username

pswd – is the password

database – is the name of the MySQL database from the Host.

If something goes wrong, for example password is not right, we must know it, and display the
error message:

if (connection == NULL)

printf(mysql_error(&mysql));

return 1;

Now we make a simple query like “SELECT * FROM mytable” and check if it has no errors, where
“mytable” is the name of wished table:

state = mysql_query(connection, “SELECT * FROM mytable”);

if (state !=0)

printf(mysql_error(connection));

return 1;

After the successful execution of the query, we must store the results somewhere:

result = mysql_store_result(connection);

Using mysql_num_rows function, we can get number of rows from result:

printf(“Rows:%d\n”,mysql_num_rows(result));

Using while statement and mysql_fetch_row functions, it possible to process each row in the
result set:

while ( ( row=mysql_fetch_row(result)) != NULL )

{
printf(” %s, %s\n”, (row[0] ? row[0] : “NULL”), (row[1] ? row[1] : “NULL” ));

At the end, we must free the memory:

mysql_free_result(result);

mysql_close(connection);

IMPORTANT!!! How to make this code under Linux?

g++ test.cpp -I/usr/include/mysql /usr/lib/mysql/libmysqlclient.so

Enjoy!

October 18, 2007  12 Replies

« Previous Next »

Leave a comment

Honey on May 29, 2008 at 9:38 am

I have still some confussion HOW CAN I CONNECT MYSQL IN BECKEND TO MY


PROJECT……
Plzzzzzzzzz some one help out

 Reply

StianXXs on August 25, 2008 at 1:44 pm

Hi
I get this ERROR:
`MYSQL_RES’ undeclared (first use this function)
What wrong?

HELP
Contact me on stian.instebo@gmail.com

 Reply
A programmer on October 14, 2008 at 6:32 am

Post the code without the comments for easy c/p

 Reply

vinod on January 5, 2009 at 10:11 am

there is no file like mysql.h and no directory /usr/include/mysql


……………….
can u tell me how can i get that????

Do i need to install some kinda connector like mysql++

 Reply

Bogdan on March 14, 2009 at 10:42 am

Hy. I`ve read your example code and I tried to create a .cpp file with. When I try to compile
it says that MYSQL/MYSQL_RES/MYSQL_ROW was not declared in this scope.
I tried to change the include line from #inlcude to #include but no good results.

I`m using Ubuntu 8.04 with Eclipse C/C++ IDE.


Can you tell me what could be the problem and if I can connect to a MySQL database
creating the program through a IDE?

 Reply

testalucida on May 30, 2009 at 9:30 pm

great first step to working with mysql – thank you!

 Reply

aldo on June 24, 2009 at 3:10 am

thanks man!
i will try your code

 Reply
righteous on July 18, 2009 at 1:18 pm

Good, but this is the C API. You should be using the Connecter C++ API from MySQL

http://codediaries.blogspot.com/2009/07/mysql-connector-c-example-windows-
clexe.html

 Reply

dave nicholas on December 27, 2009 at 1:09 pm

It’s worth mentioning when using your IDE you can also add the mysqlclient to the
libraries list.

 Reply

CoolMan on July 5, 2010 at 12:25 am

Cool! I needed this.

Thanks man!

 Reply

Alex on September 5, 2010 at 5:08 pm

Hi , good post .
only tht you used the SQL c API .
there is also a C++ only Connector which is object oriented
that worth a mention.

http://www.mysql.com/downloads/connector/cpp/

 Reply

Etienne on July 10, 2011 at 1:06 pm

Hi Vladimir,

Thank you, working perfectly here, I used a slightly different compile command as the
one given gave some problems on my linux box, the compile command for anyone else
to try: g++ test.cpp -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient_r -o test
 Reply

View Full Site

Blog at WordPress.com.

You might also like