求救,这样的问题怎么解决?哪位大虾帮忙分析一下?
出现了这样的问题,老是说我连接不上数据库?请教???
int main( int argc, char * argv[] )
{
char szSQL[ 200 ], aszFlds[ 25 ][ 25 ], * pszT, szDB[ 50 ] ;
int i, j, k, l, x ;
MYSQL * firstdata ;
MYSQL_RES * res ;
MYSQL_FIELD * fd ;
MYSQL_ROW row ;
//....just curious....
printf( "sizeof( MYSQL ) == %d\n", sizeof( MYSQL ) ) ;
if ( argc == 2 )
{
strcpy( szDB, argv[ 1 ] ) ;
strcpy( szSQL, DEFALT_SQL_STMT ) ;
if (!strcmp(szDB,"--debug"))
{
strcpy( szDB, "mysql" ) ;
printf("Some mysql struct information (size and offset):\n");
printf("net:\t%3d %3d\n",sizeof(firstdata->net),offsetof(MYSQL,net));
printf("host:\t%3d %3d\n",sizeof(firstdata->host),offsetof(MYSQL,host));
printf("port:\t%3d %3d\n",sizeof(firstdata->port),offsetof(MYSQL,port));
printf("protocol_version:\t%3d %3d\n",sizeof(firstdata->protocol_version),
offsetof(MYSQL,protocol_version));
printf("thread_id:\t%3d %3d\n",sizeof(firstdata->thread_id),
offsetof(MYSQL,thread_id));
printf("affected_rows:\t%3d %3d\n",sizeof(firstdata->affected_rows),
offsetof(MYSQL,affected_rows));
printf("packet_length:\t%3d %3d\n",sizeof(firstdata->packet_length),
offsetof(MYSQL,packet_length));
printf("status:\t%3d %3d\n",sizeof(firstdata->status),
offsetof(MYSQL,status));
printf("fields:\t%3d %3d\n",sizeof(firstdata->fields),
offsetof(MYSQL,fields));
printf("field_alloc:\t%3d %3d\n",sizeof(firstdata->field_alloc),
offsetof(MYSQL,field_alloc));
printf("free_me:\t%3d %3d\n",sizeof(firstdata->free_me),
offsetof(MYSQL,free_me));
printf("options:\t%3d %3d\n",sizeof(firstdata->options),
offsetof(MYSQL,options));
puts("");
}
}
else if ( argc > 2 ) {
strcpy( szDB, argv[ 1 ] ) ;
strcpy( szSQL, argv[ 2 ] ) ;
}
else {
strcpy( szDB, "mysql" ) ;
strcpy( szSQL, DEFALT_SQL_STMT ) ;
}
//....
if ( (firstdata = mysql_init((MYSQL*) 0)) &&
mysql_real_connect( firstdata, NULL, NULL, NULL, NULL, MYSQL_PORT,
NULL, 0 ) )
{
if ( mysql_select_db( firstdata, szDB ) < 0 )
{
printf( "Can't select the %s database !\n", szDB ) ;
mysql_close( firstdata ) ;
return 2 ;
}
}
else
{
printf( "Can't connect to the mysql server on port %d !\n",
MYSQL_PORT ) ;
mysql_close( firstdata ) ;
return 1 ;
}
//....
if ( ! mysql_query( firstdata, szSQL ) )
{
res = mysql_store_result( firstdata ) ;
i = (int) mysql_num_rows( res ) ;
l = 1 ;
printf( "Query: %s\nNumber of records found: %ld\n", szSQL, i ) ;
//....we can get the field-specific characteristics here....
for ( x = 0 ; fd = mysql_fetch_field( res ) ; x++ )
strcpy( aszFlds[ x ], fd->name ) ;
//....
while ( row = mysql_fetch_row( res ) )
{
j = mysql_num_fields( res ) ;
printf( "Record #%ld:-\n", l++ ) ;
for ( k = 0 ; k < j ; k++ )
printf( " Fld #%d (%s): %s\n", k + 1, aszFlds[ k ],
(((row[k]==NULL)||(!strlen(row[k])))?"NULL":row[k])) ;
puts( "==============================\n" ) ;
}
mysql_free_result( res ) ;
}
else printf( "Couldn't execute %s on the server !\n", szSQL ) ;
//....
puts( "==== Diagnostic info ====" ) ;
pszT = mysql_get_client_info() ;
printf( "Client info: %s\n", pszT ) ;
//....
pszT = mysql_get_host_info( firstdata ) ;
printf( "Host info: %s\n", pszT ) ;
//....
pszT = mysql_get_server_info( firstdata ) ;
printf( "Server info: %s\n", pszT ) ;
//....
res = mysql_list_processes( firstdata ) ; l = 1 ;
if (res)
{
for ( x = 0 ; fd = mysql_fetch_field( res ) ; x++ )
strcpy( aszFlds[ x ], fd->name ) ;
while ( row = mysql_fetch_row( res ) ) {
j = mysql_num_fields( res ) ;
printf( "Process #%ld:-\n", l++ ) ;
for ( k = 0 ; k < j ; k++ )
printf( " Fld #%d (%s): %s\n", k + 1, aszFlds[ k ],
(((row[k]==NULL)||(!strlen(row[k])))?"NULL":row[k])) ;
puts( "==============================\n" ) ;
}
}
else
{
printf("Got error %s when retreiving processlist\n",mysql_error(firstdata));
}
//....
res = mysql_list_tables( firstdata, "%" ) ; l = 1 ;
for ( x = 0 ; fd = mysql_fetch_field( res ) ; x++ )
strcpy( aszFlds[ x ], fd->name ) ;
while ( row = mysql_fetch_row( res ) ) {
j = mysql_num_fields( res ) ;
printf( "Table #%ld:-\n", l++ ) ;
for ( k = 0 ; k < j ; k++ )
printf( " Fld #%d (%s): %s\n", k + 1, aszFlds[ k ],
(((row[k]==NULL)||(!strlen(row[k])))?"NULL":row[k])) ;
puts( "==============================\n" ) ;
}
//....
pszT = mysql_stat( firstdata ) ;
puts( pszT ) ;
//....
mysql_close( firstdata ) ;
return 0 ;
}