mysql

来源:互联网 发布:openstack源码安装 编辑:程序博客网 时间:2024/06/16 19:49

int check_table( char * tablename, char * date)

{

MYSQL mysql;

MYSQL_RES *result = NULL;

MYSQL_ROW row;

char table_name[128] = {0x0};

char dbquery[1024] = {0x0};

//get table name

sprintf( table_name, "%s_%s", tablename, date);

if( mysql_init( &mysql) == NULL)

{

printf( "%s\n", mysql_error(&mysql));

goto NOTFOUND;

}

if( mysql_real_connect( &mysql, g_DB_result_host, "root", "123456", "analyse_flow", g_DB_result_port, NULL, 0) == NULL)

{

printf( "%s\n", mysql_error(&mysql));

goto NOTFOUND;

}

sprintf( dbquery, "show tables like \"%s\";", table_name);

if( mysql_query( &mysql, dbquery) != 0)

{

printf( "%s\n", mysql_error(&mysql));

goto NOTFOUND;

}

if( (result = mysql_store_result( &mysql)) == NULL)

{

printf( "%s\n", mysql_error(&mysql));

goto NOTFOUND;

}

if( (row = mysql_fetch_row(result)) == NULL)

{

sprintf( dbquery, "CREATE TABLE %s( hash char(40) not null primary key);", table_name);

if( mysql_query( &mysql, dbquery) != 0)

{

//can not create table

printf( "%s\n", mysql_error(&mysql));

goto NOTFOUND;

}

}

if( result != NULL) mysql_free_result (result);

mysql_close( &mysql);

return 1;

NOTFOUND:

if( result != NULL) mysql_free_result (result);

mysql_close( &mysql);

return 0;

}

原创粉丝点击