Hi,

   Attached is my test program while exercising the C API of the MySQL.
Of course I have replaced the host name, user name, password and database
name with dummy ones.

   I am using RH 6.1. The MySQL version is 3.22.

   Essentially you need to do

#include <mysql.h>
...
main()
{
  MYSQL mysql;
  MYSQL_RES *result;
...
  mysql_init(&mysql);
...
  if (!mysql_real_connect(&mysql, "[Host]", "[user]", "[passwd]",
"[database]",0,NULL,0))
  {
      fprintf(stderr, "Failed to connect to database: Error: %s\n",
            mysql_error(&mysql));
  }
  else
  {
...
     sprintf(sqlcomd, "SELECT * FROM good_runs WHERE run_number = %s",
runnumber);
     mysql_query(&mysql, sqlcomd);
     result = mysql_use_result(&mysql);
...
  }

}


On Thu, 20 Sep 2001, HaB JacKaL wrote:

> Can someone give me a quick *complete* example code of setting up a
> connection, performing a small query, and reading back the result?
>
>
> The docs conflict in a few places as far as how to declare the data types.
> (in some places, they are pointers, in others they are not).  Also, the
> examples, sadly, just aren't very good.  And there is no example for
> mysql_use_result().  (the example code uses mysql_store_result())
>
> I have tried every combination of pointers and regular datatypes I can think
> of, but I'm still generating errors on compile.
>
> Running on linux (RH 7.1), with MySQL 3.23.39.
>
> I'm using this:
>
>   MYSQL mysql;
>   MYSQL_RES *result;
>   MYSQL_ROW row;
>   int error;
>
>   if(!mysql_init(&mysql))
>     {
>       printf("init failed!\n");
>       exit(1);
>     }
>
>   if(!mysql_real_connect(&mysql, "localhost",
>                          "user", "passwd", "dbname", 0, "", 0))
>     {
>       printf("doh!  connect failed...\n");
>       exit(1);
>     }
>   else
>     {
>       error = mysql_select_db(&mysql, "label");
>       error = mysql_query(&mysql, "SELECT * FROM _countries");
>       result = mysql_use_result(&mysql);
>       while((row = mysql_fetch_row(result)))
>         {
>
> mysql_init() is returning fine...
>
> The connection is failing.  I know the username/password combo works, since
> I can use it on the command line.  The client program is being run on the
> same linux machine as the mysql server.
>
> Any other ideas?
>
> Does mysqld have a debug mode?  Some way I can see if the client is even
> trying to connect, and what data the server is getting?
>
> Also, are the datatypes declared correctly for the rest of the code?  The
> docs conflict as to whether or not result should be declared as a pointer or
> not.
>
>
> Thanks.
>
> -HaB
>
>
>
> _________________________________________________________________
> Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp
>
>
> ---------------------------------------------------------------------
> Before posting, please check:
>    http://www.mysql.com/manual.php   (the manual)
>    http://lists.mysql.com/           (the list archive)
>
> To request this thread, e-mail <[EMAIL PROTECTED]>
> To unsubscribe, e-mail <[EMAIL PROTECTED]>
> Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php
>

-- 
   Best regards,    Yen-Chu Chen
                    [EMAIL PROTECTED]
                    Office: (630) 840-5403,  FAX: (630) 840-2968
                    (886)-(2)-2789-9681 (Inst. of Phys., Academia Sinica)
#include <mysql.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>

main()
{
  MYSQL mysql;
  MYSQL_RES *result;
  MYSQL_FIELD *fields;
  MYSQL_ROW row;

  unsigned long *lengths;

  char *runnumber=" ";
  char *msg=" ";
  char run[10];
  char evn[10];

  int skip;
  int first;

  char sqlcomd[200];

  unsigned int num_fields;
  unsigned int i;

  int msgsize;

  mysql_init(&mysql);
  fprintf(stderr, "Try to connect to database ...\n");
  if (!mysql_real_connect(&mysql, "[host]", "[user]", "[passwd]", 
"[database]",0,NULL,0))
  {
      fprintf(stderr, "Failed to connect to database: Error: %s\n",
            mysql_error(&mysql));
  }
  else 
  {

     msg = "67890|12345";
     sprintf(run, "%s", "");
     sprintf(evn, "%s", "");

     first = 1;
     skip = 0;
     for(i=0; i<20; i++)
     {
        if(msg[i] == 0)
        {
          if(msg[i+1] == 0) printf("End of string.\n");
          break;
        }

        if((msg[i] == '|') | msg[i] == ' ')
        {
           first--;
           skip = 1;
           printf("msg[%d] = %c, Got it!!! \n", i, msg[i]);
        }
        else
        {
           skip = 0;
           printf("msg[%d] = %d \n", i, msg[i]);
        }

        if(skip == 0)
        {
           if(first == 1) 
           {
              sprintf(run, "%s%c", run, msg[i]);
           }
           else
           {
              sprintf(evn, "%s%c", evn, msg[i]);
           }
        }

     } 

     printf("Run number is %s. \n", run);
     printf("Event number is %s. \n", evn);

     runnumber = run;

     sprintf(sqlcomd, "INSERT INTO good_runs SET run_number = %s", runnumber); 
     sprintf(sqlcomd, "%s , period = 'August_2001'", sqlcomd);
     sprintf(sqlcomd, "%s , creation_time = NOW()", sqlcomd);
     sprintf(sqlcomd, "%s , summary_events = %s", sqlcomd, evn);
     printf("%s \n", sqlcomd);
     mysql_query(&mysql, sqlcomd);
   

     sprintf(sqlcomd, "SELECT * FROM good_runs WHERE run_number = %s", runnumber);
     mysql_query(&mysql, sqlcomd);
     result = mysql_use_result(&mysql);

     num_fields = mysql_num_fields(result);
     fields = mysql_fetch_fields(result);
     lengths = mysql_fetch_lengths(result);
     while ((row = mysql_fetch_row(result)))
     {
        for(i = 0; i < num_fields; i++)
        {
           printf("Field %u is %s = [%s] \n", i, fields[i].name, row[i]);
        /* printf("[%.*s] ", (int) lengths[i], row[i] ? row[i] : "NULL"); */
        }
     }

  
     fprintf(stderr, "Delete the testing entry. \n");
     sprintf(sqlcomd, "DELETE FROM good_runs WHERE run_number = %s", runnumber);
     mysql_query(&mysql, sqlcomd);

   

     fprintf(stderr, "Close the connection to database. \n");
     mysql_close(&mysql);
  }
}
---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to