mysql and C++

This is a discussion on mysql and C++ within the Linux Programming forums, part of the Platform Specific Boards category; Hi everyone, I'm a C++ fan and want to write an application on Linux box, Redhat, to access a database ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    32

    mysql and C++

    Hi everyone, I'm a C++ fan and want to write an application on Linux box, Redhat, to access a database

    I've been searching this site and many others for hours, and it seems my best bet is using mysql.

    I still very new to this type of programing, and I specifically want to stay command line and access the db, and I am finding it hard to find books or web site with basic begginers code to do this, so that I can learn what libraries, ect...

    can anyone help me? I've been to mysql.com, searched google up and down, I think I am not familiar enough with what I trying to do to find the right place to start???

    Thanks for any help, everyone at cboard have been really helpful.
    I'd like to put Murphy and his laws in headlock sometimes!

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Read the tutorial section http://www.mysql.com/documentation/m....html#Tutorial of the mysql website. Learn what commands you need to give to get the information you want.

    After that read the API section http://www.mysql.com/documentation/m...Clients.html#C to learn the calls.

    A basic program is setup as such:

    1) Initalize the database connection
    2) Connect to the server
    3) Select the database to use
    4) Give it commands with mysql_query or mysql_real_query

    Below is a little program. It works but could use some minor tweaking to make the output prettier.
    Code:
    #include <stdio.h>
    #include <mysql/mysql.h>
    
    int main (void)
    {
      MYSQL db_conn;
      MYSQL_RES *result;
      MYSQL_ROW row;
      MYSQL_FIELD *fields;
      unsigned int numrows;
      unsigned int numfields;
      int c;
      char user[] = "tester";
      char password[] = "tester";
      char host[] = "localhost";
      char dbname[] = "Pics";
      char buffer[BUFSIZ];
    
      mysql_init(&db_conn);
    
      if ( mysql_real_connect (&db_conn, host, user, password, dbname, 0, NULL, 0) == NULL)
      {
        fprintf(stderr, "Could not connect to mysql server.  Error %s\n", mysql_error(&db_conn));
        return 0;
      }
    
      puts ("Connected");
    
      printf("Client Info: %s\n", mysql_get_client_info() );
    
      sprintf(buffer, "SELECT * FROM %s", "Events");
    
      if ( mysql_query(&db_conn, buffer) )
      {
        fprintf(stderr, "Could not execute the command\n%s\nrecieved the error \n%s\n", buffer, mysql_error(&db_conn));
      }
      else
      {
        result = mysql_store_result(&db_conn);
        if ( (numrows = mysql_num_rows(result)) )
        {
          printf("There are %d rows in this result which are:\n", numrows);
          numfields = mysql_num_fields(result);
          fields = mysql_fetch_fields(result);
    
          for (c=0; c<numfields; c++)
            printf("%s\t", fields[c].name);
          putchar('\n');
    
          while ( (row = mysql_fetch_row(result)) )
          {
            for (c=0; c<numfields; c++)
              printf("%s\t", row[c]);
            putchar('\n');
          }
    
        }
        else
          puts("Empty Set");
    
        mysql_free_result(result);
      }
    
      return 0;
    }

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    32
    Thats awesome, thank you very much, I will start reading right now, thanks again and thanks for the sample code, reading concepts is always necessary, but the sample code really helps me get an idea of whats going on.

    I'd like to put Murphy and his laws in headlock sometimes!

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21