Chinaunix
标题:
c + mysql简单例程
[打印本页]
作者:
大隐隐于床
时间:
2009-03-24 18:40
标题:
c + mysql简单例程
c + mysql 简单使用例程。
检查一个表是否存在,不存在则创建。
附:MySQL中文参考手册
文件:
MySQL中文参考手册.rar
大小:
604KB
下载:
下载
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;
}
本文来自ChinaUnix博客,如果查看原文请点:
http://blog.chinaunix.net/u3/91335/showart_1878269.html
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2