I'm exploring basic C/C++ programming in accessing MySQL server.  I'd
prefer PostgreSQL in C++, but the only tutorial/example I found easily
was MySQL in C.  I know very little about C/C++ or SQL; just barely
enough that I see a little more than jibberish... I see intelligent
jibberish. :-P  I'm running MySQL and KDevelop on a Mandrake 9.1 box.  I
know this is quite off topic, but I figured I'd try it on this awesome
list anyway. :-P Anywho, this is the example I found:

<code sample>

#include &ltmysql/mysql.h>
#include &ltstdio.h>

 
int main(){
 
   MYSQL mysql;
   MYSQL_ROW row; 
   MYSQL_RES *result; 
    
   unsigned int num_fields;
   unsigned int i;

   mysql_init(&mysql);

   if
(!mysql_real_connect(&mysql,"localhost","root","","MyDatabase",0,NULL,0))
   {
      fprintf(stderr, "Failed to connect to database: Error: %s\n",
           mysql_error(&mysql)); 
   }
   else { 
      if(mysql_query(&mysql, "SELECT * FROM my_table"));
         //here goes the error message :o) 
      else {
         result = mysql_store_result(&mysql); 
         num_fields = mysql_num_fields(result);
         while ((row = mysql_fetch_row(result))) 
         {
               unsigned long *lengths; 
               lengths = mysql_fetch_lengths(result);
               for(i = 0; i < num_fields; i++) 
               {
                       printf("[%.*s] \t", (int) lengths[i], row[i] ?
row[i] : "NULL");
               }    
               printf("\n"); 
         }
      } 
   }
 
   return 0;

}

</code sample>

I replaced the "MyDatable", "My_table" and root password sections with
data specific to my server.  Keep in mind that I'm newbie to about
everything SQL/Linux/C/C++.  On compiling I get hundreds of lines of
"stray '\240' in program" intermingled with other various errors, and
the build fails.  Anyone have a clue whats up?


____________________________________________________
Want to buy your Pack or Services from MandrakeSoft? 
Go to http://www.mandrakestore.com
Join the Club : http://www.mandrakeclub.com
____________________________________________________

Reply via email to