home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 January / Chip_2001-01_cd1.bin / tema / mysql / mysql-3.23.28g-win.exe / DATA1.CAB / Examples / examples / libmysqltest / myTest.c next >
C/C++ Source or Header  |  2000-11-22  |  5KB  |  170 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 ], * pszT, szDB[ 50 ] ;
  35.   int            i, j, k, l, x ;
  36.   MYSQL        * myData ;
  37.   MYSQL_RES    * res ;
  38.   MYSQL_FIELD    * fd ;
  39.   MYSQL_ROW    row ;
  40.  
  41.   //....just curious....
  42.   printf( "sizeof( MYSQL ) == %d\n", sizeof( MYSQL ) ) ;
  43.   if ( argc == 2 )
  44.     {
  45.       strcpy( szDB, argv[ 1 ] ) ;
  46.       strcpy( szSQL, DEFALT_SQL_STMT ) ;
  47.       if (!strcmp(szDB,"--debug"))
  48.       {
  49.     strcpy( szDB, "mysql" ) ;
  50.     printf("Some mysql struct information (size and offset):\n");
  51.     printf("net:\t%3d %3d\n",sizeof(myData->net),offsetof(MYSQL,net));
  52.     printf("host:\t%3d %3d\n",sizeof(myData->host),offsetof(MYSQL,host));
  53.     printf("port:\t%3d %3d\n",sizeof(myData->port),offsetof(MYSQL,port));
  54.     printf("protocol_version:\t%3d %3d\n",sizeof(myData->protocol_version),
  55.            offsetof(MYSQL,protocol_version));
  56.     printf("thread_id:\t%3d %3d\n",sizeof(myData->thread_id),
  57.            offsetof(MYSQL,thread_id));
  58.     printf("affected_rows:\t%3d %3d\n",sizeof(myData->affected_rows),
  59.            offsetof(MYSQL,affected_rows));
  60.     printf("packet_length:\t%3d %3d\n",sizeof(myData->packet_length),
  61.            offsetof(MYSQL,packet_length));
  62.     printf("status:\t%3d %3d\n",sizeof(myData->status),
  63.            offsetof(MYSQL,status));
  64.     printf("fields:\t%3d %3d\n",sizeof(myData->fields),
  65.            offsetof(MYSQL,fields));
  66.     printf("field_alloc:\t%3d %3d\n",sizeof(myData->field_alloc),
  67.            offsetof(MYSQL,field_alloc));
  68.     printf("free_me:\t%3d %3d\n",sizeof(myData->free_me),
  69.            offsetof(MYSQL,free_me));
  70.     printf("options:\t%3d %3d\n",sizeof(myData->options),
  71.            offsetof(MYSQL,options));
  72.     puts("");
  73.       }
  74.     }        
  75.   else if ( argc > 2 ) {
  76.     strcpy( szDB, argv[ 1 ] ) ;
  77.     strcpy( szSQL, argv[ 2 ] ) ;
  78.   }
  79.   else {
  80.     strcpy( szDB, "mysql" ) ;
  81.     strcpy( szSQL, DEFALT_SQL_STMT ) ;
  82.   }
  83.   //....
  84.           
  85.   if ( (myData = mysql_init((MYSQL*) 0)) && 
  86.        mysql_real_connect( myData, NULL, NULL, NULL, NULL, MYSQL_PORT,
  87.                NULL, 0 ) )
  88.     {
  89.       if ( mysql_select_db( myData, szDB ) < 0 ) {
  90.     printf( "Can't select the %s database !\n", szDB ) ;
  91.     mysql_close( myData ) ;
  92.     return 2 ;
  93.       }
  94.     }
  95.   else {
  96.     printf( "Can't connect to the mysql server on port %d !\n",
  97.         MYSQL_PORT ) ;
  98.     mysql_close( myData ) ;
  99.     return 1 ;
  100.   }
  101.   //....
  102.   if ( ! mysql_query( myData, szSQL ) ) {
  103.     res = mysql_store_result( myData ) ;
  104.     i = (int) mysql_num_rows( res ) ; l = 1 ;
  105.     printf( "Query:  %s\nNumber of records found:  %ld\n", szSQL, i ) ;
  106.     //....we can get the field-specific characteristics here....
  107.     for ( x = 0 ; fd = mysql_fetch_field( res ) ; x++ )
  108.       strcpy( aszFlds[ x ], fd->name ) ;
  109.     //....
  110.     while ( row = mysql_fetch_row( res ) ) {
  111.       j = mysql_num_fields( res ) ;
  112.       printf( "Record #%ld:-\n", l++ ) ;
  113.       for ( k = 0 ; k < j ; k++ )
  114.     printf( "  Fld #%d (%s): %s\n", k + 1, aszFlds[ k ],
  115.         (((row[k]==NULL)||(!strlen(row[k])))?"NULL":row[k])) ;
  116.       puts( "==============================\n" ) ;
  117.     }
  118.     mysql_free_result( res ) ;
  119.   }
  120.   else printf( "Couldn't execute %s on the server !\n", szSQL ) ;
  121.   //....
  122.   puts( "====  Diagnostic info  ====" ) ;
  123.   pszT = mysql_get_client_info() ;
  124.   printf( "Client info: %s\n", pszT ) ;
  125.   //....
  126.   pszT = mysql_get_host_info( myData ) ;
  127.   printf( "Host info: %s\n", pszT ) ;
  128.   //....
  129.   pszT = mysql_get_server_info( myData ) ;
  130.   printf( "Server info: %s\n", pszT ) ;
  131.   //....
  132.   res = mysql_list_processes( myData ) ; l = 1 ;
  133.   if (res)
  134.     {
  135.       for ( x = 0 ; fd = mysql_fetch_field( res ) ; x++ )
  136.     strcpy( aszFlds[ x ], fd->name ) ;
  137.       while ( row = mysql_fetch_row( res ) ) {
  138.     j = mysql_num_fields( res ) ;
  139.     printf( "Process #%ld:-\n", l++ ) ;
  140.     for ( k = 0 ; k < j ; k++ )
  141.       printf( "  Fld #%d (%s): %s\n", k + 1, aszFlds[ k ],
  142.           (((row[k]==NULL)||(!strlen(row[k])))?"NULL":row[k])) ;
  143.     puts( "==============================\n" ) ;
  144.       }
  145.     }
  146.   else
  147.     {
  148.       printf("Got error %s when retreiving processlist\n",mysql_error(myData));
  149.     }
  150.   //....
  151.   res = mysql_list_tables( myData, "%" ) ; l = 1 ;
  152.   for ( x = 0 ; fd = mysql_fetch_field( res ) ; x++ )
  153.     strcpy( aszFlds[ x ], fd->name ) ;
  154.   while ( row = mysql_fetch_row( res ) ) {
  155.     j = mysql_num_fields( res ) ;
  156.     printf( "Table #%ld:-\n", l++ ) ;
  157.     for ( k = 0 ; k < j ; k++ )
  158.       printf( "  Fld #%d (%s): %s\n", k + 1, aszFlds[ k ],
  159.           (((row[k]==NULL)||(!strlen(row[k])))?"NULL":row[k])) ;
  160.     puts( "==============================\n" ) ;
  161.   }
  162.   //....
  163.   pszT = mysql_stat( myData ) ;
  164.   puts( pszT ) ;
  165.   //....
  166.   mysql_close( myData ) ;
  167.   return 0 ;
  168.  
  169. }
  170.