1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

My SQL and C++

Discussion in 'C++' started by techme, Mar 23, 2010.

  1. techme

    techme New Member

    Joined:
    Feb 15, 2010
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    I am working in C and I wanna connect my app in C with MySql.
    Is there a app library to use with C to connect for mysql?
    I had the library for mysql++ but this work more properly with c++ and I am using C?
    Any comments?
     
  2. pankaj.sea

    pankaj.sea New Member

    Joined:
    Apr 6, 2009
    Messages:
    461
    Likes Received:
    13
    Trophy Points:
    0
    Occupation:
    Web Developer
    Location:
    Kolkata
    Home Page:
  3. inspiration

    inspiration New Member

    Joined:
    Feb 15, 2010
    Messages:
    85
    Likes Received:
    0
    Trophy Points:
    0
    take it from me.....mysql and c are a pain in the ***. there is an api for mysql, though, but there are a few things you might want to know:

    /*all this applies to unix*/

    to compile, type this:

    gcc -g -o -L/*directory*/lib -I/*directory*/include -lmysqlclient -lm

    where I put *directory*, put the location of your mysql folder. This should be something like /usr/local/mysql/

    then....here are some code snippets you can use:


    /*connect to mysql*/
    mysql_init(&mysql);
    connection = mysql_connect(&mysql, "localhost", "9841860u", "9841860u");

    /*check for a connection error*/
    if(connection == NULL)
    {
    /*print error message*/
    printf(mysql_error(&mysql));
    }


    /*select a database*/
    mysql_select_db(&mysql, "Nag");

    /*here you create a string to pass as a query...you have to do it this way (I think) because you can't pass mysql a variable from C*/
    strcpy(query, "SELECT column FROM table WHERE variable = ");
    strcat(query, x);
    state = mysql_query(connection, query);
    if(state != 0)
    {
    printf(mysql_error(connection));
    }

    /*this will give you a result set (if you are expecting a result)...you can go through that with row=mysql_fetch_row(result)*/

    /*before you can call another query that will return a result set, you must free the result set*/
    mysql_free_result(result);

    /*close the mysql connection*/
    mysql_close(connection);

    there are a whole load of functions and structures used, just go through the c api section of the mysql manual.....you can get that online

    hope I was of help,

    good luck,
     
  4. techme

    techme New Member

    Joined:
    Feb 15, 2010
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    how about windows?
    how to compile them with bcc?
     
  5. inspiration

    inspiration New Member

    Joined:
    Feb 15, 2010
    Messages:
    85
    Likes Received:
    0
    Trophy Points:
    0
    sorry techme!
    but I don't use windows!
    so I can't tell!
     
  6. techme

    techme New Member

    Joined:
    Feb 15, 2010
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    :(
    Too Bad!
    any way! Thanks!
    :)
     
  7. davidk

    davidk New Member

    Joined:
    Mar 25, 2010
    Messages:
    16
    Likes Received:
    1
    Trophy Points:
    0
    Home Page:
    The code will be the same on Windows. You just need to install MySQL server on your machine in case you wanna test your code on it. Once you have installed MySQL you can find the libraries within its directory where it has been installed. Use them to build your app on Windows.
     

Share This Page