home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2004 March / PCWMAR04.iso / Software / Resources / MySQL / Windows / data1.cab / Development / examples / libmysqltest / myTest.c next >
Encoding:
C/C++ Source or Header  |  2003-10-20  |  5.3 KB  |  171 lines

  1. /*C4*/
  2. /****************************************************************/
  3. /*    Author:    Jethro Wright, III    TS :  3/ 4/1998  9:15    */
  4. /*    Date:    02/18/1998                    */
  5. /*    mytest.c :  do some testing of the libmySQL.DLL....    */
  6. /*                                */
  7. /*    History:                        */
  8. /*        02/18/1998  jw3  also sprach zarathustra....    */
  9. /****************************************************************/
  10.  
  11.  
  12. #include        <windows.h>
  13. #include    <stdio.h>
  14. #include    <string.h>
  15.  
  16. #include    <mysql.h>
  17.  
  18. #define        DEFALT_SQL_STMT    "SELECT * FROM db"
  19. #ifndef offsetof
  20. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  21. #endif
  22.  
  23.  
  24. /********************************************************
  25. **
  26. **        main  :-
  27. **
  28. ********************************************************/
  29.  
  30. int
  31. main( int argc, char * argv[] )
  32. {
  33.  
  34.   char        szSQL[ 200 ], aszFlds[ 25 ][ 25 ], szDB[ 50 ] ;
  35.   const  char   *pszT;
  36.   int            i, j, k, l, x ;
  37.   MYSQL        * myData ;
  38.   MYSQL_RES    * res ;
  39.   MYSQL_FIELD    * fd ;
  40.   MYSQL_ROW    row ;
  41.  
  42.   //....just curious....
  43.   printf( "sizeof( MYSQL ) == %d\n", sizeof( MYSQL ) ) ;
  44.   if ( argc == 2 )
  45.     {
  46.       strcpy( szDB, argv[ 1 ] ) ;
  47.       strcpy( szSQL, DEFALT_SQL_STMT ) ;
  48.       if (!strcmp(szDB,"--debug"))
  49.       {
  50.     strcpy( szDB, "mysql" ) ;
  51.     printf("Some mysql struct information (size and offset):\n");
  52.     printf("net:\t%3d %3d\n",sizeof(myData->net),offsetof(MYSQL,net));
  53.     printf("host:\t%3d %3d\n",sizeof(myData->host),offsetof(MYSQL,host));
  54.     printf("port:\t%3d %3d\n",sizeof(myData->port),offsetof(MYSQL,port));
  55.     printf("protocol_version:\t%3d %3d\n",sizeof(myData->protocol_version),
  56.            offsetof(MYSQL,protocol_version));
  57.     printf("thread_id:\t%3d %3d\n",sizeof(myData->thread_id),
  58.            offsetof(MYSQL,thread_id));
  59.     printf("affected_rows:\t%3d %3d\n",sizeof(myData->affected_rows),
  60.            offsetof(MYSQL,affected_rows));
  61.     printf("packet_length:\t%3d %3d\n",sizeof(myData->packet_length),
  62.            offsetof(MYSQL,packet_length));
  63.     printf("status:\t%3d %3d\n",sizeof(myData->status),
  64.            offsetof(MYSQL,status));
  65.     printf("fields:\t%3d %3d\n",sizeof(myData->fields),
  66.            offsetof(MYSQL,fields));
  67.     printf("field_alloc:\t%3d %3d\n",sizeof(myData->field_alloc),
  68.            offsetof(MYSQL,field_alloc));
  69.     printf("free_me:\t%3d %3d\n",sizeof(myData->free_me),
  70.            offsetof(MYSQL,free_me));
  71.     printf("options:\t%3d %3d\n",sizeof(myData->options),
  72.            offsetof(MYSQL,options));
  73.     puts("");
  74.       }
  75.     }        
  76.   else if ( argc > 2 ) {
  77.     strcpy( szDB, argv[ 1 ] ) ;
  78.     strcpy( szSQL, argv[ 2 ] ) ;
  79.   }
  80.   else {
  81.     strcpy( szDB, "mysql" ) ;
  82.     strcpy( szSQL, DEFALT_SQL_STMT ) ;
  83.   }
  84.   //....
  85.           
  86.   if ( (myData = mysql_init((MYSQL*) 0)) && 
  87.        mysql_real_connect( myData, NULL, NULL, NULL, NULL, MYSQL_PORT,
  88.                NULL, 0 ) )
  89.     {
  90.       if ( mysql_select_db( myData, szDB ) < 0 ) {
  91.     printf( "Can't select the %s database !\n", szDB ) ;
  92.     mysql_close( myData ) ;
  93.     return 2 ;
  94.       }
  95.     }
  96.   else {
  97.     printf( "Can't connect to the mysql server on port %d !\n",
  98.         MYSQL_PORT ) ;
  99.     mysql_close( myData ) ;
  100.     return 1 ;
  101.   }
  102.   //....
  103.   if ( ! mysql_query( myData, szSQL ) ) {
  104.     res = mysql_store_result( myData ) ;
  105.     i = (int) mysql_num_rows( res ) ; l = 1 ;
  106.     printf( "Query:  %s\nNumber of records found:  %ld\n", szSQL, i ) ;
  107.     //....we can get the field-specific characteristics here....
  108.     for ( x = 0 ; fd = mysql_fetch_field( res ) ; x++ )
  109.       strcpy( aszFlds[ x ], fd->name ) ;
  110.     //....
  111.     while ( row = mysql_fetch_row( res ) ) {
  112.       j = mysql_num_fields( res ) ;
  113.       printf( "Record #%ld:-\n", l++ ) ;
  114.       for ( k = 0 ; k < j ; k++ )
  115.     printf( "  Fld #%d (%s): %s\n", k + 1, aszFlds[ k ],
  116.         (((row[k]==NULL)||(!strlen(row[k])))?"NULL":row[k])) ;
  117.       puts( "==============================\n" ) ;
  118.     }
  119.     mysql_free_result( res ) ;
  120.   }
  121.   else printf( "Couldn't execute %s on the server !\n", szSQL ) ;
  122.   //....
  123.   puts( "====  Diagnostic info  ====" ) ;
  124.   pszT = mysql_get_client_info() ;
  125.   printf( "Client info: %s\n", pszT ) ;
  126.   //....
  127.   pszT = mysql_get_host_info( myData ) ;
  128.   printf( "Host info: %s\n", pszT ) ;
  129.   //....
  130.   pszT = mysql_get_server_info( myData ) ;
  131.   printf( "Server info: %s\n", pszT ) ;
  132.   //....
  133.   res = mysql_list_processes( myData ) ; l = 1 ;
  134.   if (res)
  135.     {
  136.       for ( x = 0 ; fd = mysql_fetch_field( res ) ; x++ )
  137.     strcpy( aszFlds[ x ], fd->name ) ;
  138.       while ( row = mysql_fetch_row( res ) ) {
  139.     j = mysql_num_fields( res ) ;
  140.     printf( "Process #%ld:-\n", l++ ) ;
  141.     for ( k = 0 ; k < j ; k++ )
  142.       printf( "  Fld #%d (%s): %s\n", k + 1, aszFlds[ k ],
  143.           (((row[k]==NULL)||(!strlen(row[k])))?"NULL":row[k])) ;
  144.     puts( "==============================\n" ) ;
  145.       }
  146.     }
  147.   else
  148.     {
  149.       printf("Got error %s when retreiving processlist\n",mysql_error(myData));
  150.     }
  151.   //....
  152.   res = mysql_list_tables( myData, "%" ) ; l = 1 ;
  153.   for ( x = 0 ; fd = mysql_fetch_field( res ) ; x++ )
  154.     strcpy( aszFlds[ x ], fd->name ) ;
  155.   while ( row = mysql_fetch_row( res ) ) {
  156.     j = mysql_num_fields( res ) ;
  157.     printf( "Table #%ld:-\n", l++ ) ;
  158.     for ( k = 0 ; k < j ; k++ )
  159.       printf( "  Fld #%d (%s): %s\n", k + 1, aszFlds[ k ],
  160.           (((row[k]==NULL)||(!strlen(row[k])))?"NULL":row[k])) ;
  161.     puts( "==============================\n" ) ;
  162.   }
  163.   //....
  164.   pszT = mysql_stat( myData ) ;
  165.   puts( pszT ) ;
  166.   //....
  167.   mysql_close( myData ) ;
  168.   return 0 ;
  169.  
  170. }
  171.