Connecting mySQL with C/C++

Jahanzeb.Khan's Avatar, Join Date: Nov 2006
Newbie Member
hey budies i need a bit of help.
Can neone guide me through the process of connecting mySQL database with C/C++, interms of API's or nething relevant... Would be thankful
pradeep's Avatar, Join Date: Apr 2005
Team Leader
Firstly you will need a couple of header files for the C program which needs to connect to a MySQL server.You'll mysql.h, which is a header file containing many standard MySQL definitions.This comes with MySQL, in case you don't have it download it from the MySQL website.
I complied the following code on a Linux machine using the cc compiler. Locate the mysql.h file using the whereis command and put the the full path in the include directive.You'll also need to specify the location of mysqlclient during compile time, like this -
Code:
cc -o mysql_c_prg mysql.c -L/usr/include/mysql -L/usr/lib/mysql -lmysqlclient
Code: C
#include <stdio.h>
 #include "/usr/include/mysql/mysql.h"
                                           
 /* C program for MySQL connectivity */
                                               
 int main(int argc, char *argv[])
 {
        MYSQL mysql;
        MYSQL_RES *result;
        MYSQL_ROW row;
 
        if(!mysql_init(&mysql))
        {
           fprintf(stderr, "Cannot initialize MySQL");
           exit(1);
        }
 
         if(!mysql_real_connect(&mysql, "db.company.com","empUser", "password", "dbName", 0, NULL, 0))
        {
           fprintf(stderr, "%d: %s \n",mysql_errno(&mysql), mysql_error(&mysql));
           exit(1);
        }
 
         if(mysql_query(&mysql,"SELECT empName,deptName,mobileNo FROM employee LIMIT 10"))
         {
             fprintf(stderr, "%d:  %s\n",mysql_errno(&mysql), mysql_error(&mysql));
         }
         else
         {
             result = mysql_store_result(&mysql);
 
             printf("%50s | %12s | %12s\n","Employee Name","Department","Mobile No");
 
             while(row = mysql_fetch_row(result))
             {
                 printf("%50s | %12s | %12s \n", row[0], row[1], row[2]);
             }
             mysql_free_result(result);
         }
 
         mysql_close(&mysql);
 }
Jahanzeb.Khan's Avatar, Join Date: Nov 2006
Newbie Member
actually am using windows platform and i need to connect mySQL db with C/C++, thanks for this much efford but still if you can help me.. i would defenitely appreciate your efforts.. thanxs and bestr regards..
pradeep's Avatar, Join Date: Apr 2005
Team Leader
Try out this link http://dev.mysql.com/doc/refman/5.0/...compiling.html