Thread: mysql select statement

  1. #1
    Registered User
    Join Date
    Nov 2006
    Location
    under your bed....
    Posts
    24

    mysql select statement

    hey all,

    recently ive been trying to run mysql queries in c++. ive successfully connect to the database using the following code:

    Code:
    MYSQL mysql;
    MYSQL *mysqlRtn;
    mysql_init(&mysql);
    mysql_options(&mysql, MYSQL_READ_DEFAULT_GROUP, "administrator");
    mysqlRtn = mysql_real_connect(&mysql, "localhost", "root", "password", "administrator", 0, NULL, 0);
    what am stuck on is how to do a select statement in c
    for example:

    SELECT * FROM <table>;

    does anyone know how i can do this in c and return all the data in an array of sorts?

    Thanks in advance.

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Create a structure that represents a record.
    Run your query, allocate enough room for an array of your structures, and fill in the array with the results. Return it. (Or use std::vector if you're using C++)

    Ideally, look up the function in the documentation for your library. Something like mysql_query() perhaps?
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Registered User
    Join Date
    Nov 2006
    Location
    under your bed....
    Posts
    24
    Thanks i got it
    use mysql_query() to run the query then use mysql_store_result() to retrieve the data.

  4. #4
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168
    i use mysql_use_result()
    because it doesnt make an temporary table and runs faster... as long as you dont have to do a lot of calulations or table manipulation.

    then you just need to fetch all the rows.
    mysql_fetch_row()

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  2. SELECT statement
    By rhysmeister in forum C Programming
    Replies: 10
    Last Post: 09-11-2004, 04:44 PM
  3. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  4. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM