Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Connecting mySQL with C/C++ (http://www.go4expert.com/forums/connecting-mysql-c-cpp-t1795/)

Jahanzeb.Khan 3Nov2006 12:36

Connecting mySQL with C/C++
 
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 3Nov2006 18:42

Re: Connecting mySQL with C/C++
 
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 4Nov2006 10:03

Re: Connecting mySQL with C/C++
 
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 4Nov2006 10:29

Re: Connecting mySQL with C/C++
 
Try out this link http://dev.mysql.com/doc/refman/5.0/...compiling.html


All times are GMT +5.5. The time now is 13:53.