Connecting mySQL with C/C++

Discussion in 'C++' started by Jahanzeb.Khan, Nov 3, 2006.

  1. Jahanzeb.Khan

    Jahanzeb.Khan New Member

    Joined:
    Nov 1, 2006
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    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:
     #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);
     }
     
  3. Jahanzeb.Khan

    Jahanzeb.Khan New Member

    Joined:
    Nov 1, 2006
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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..
     
  4. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice