mysql simple example

간단한 mysql 관련 샘플


#include    <stdio.h>

#include    <mysql.h>

#include    <errno.h>

#include    <string.h>

int main( int argc, char *argv[] ){

MYSQL mysql;

mysql_init(&mysql);

if( !mysql_real_connect( &mysql, "localhost", id, pass, dbname, 3306, NULL, 0 )){

printf("%sn", mysql_error(&mysql));

return 1;

}


printf("Mysql Connectedn");

MYSQL_RES *result;

MYSQL_ROW row;

char sql[256];

strcpy( sql, "select * from tbl_user_info" );

if( mysql_query( &mysql, sql ) != 0 ){

printf("query fail: %sn", mysql_error(&mysql));

mysql_close(&mysql);

return 1;

}

result = mysql_store_result( &mysql );

int nCount = mysql_num_rows( result );

while( (row = mysql_fetch_row( result ) ) != NULL ){

printf("%s %s %sn", row[0], row[1], row[2] );

}

mysql_free_result( result );

mysql_close(&mysql);

return 0;

}