Chinaunix首页 | 论坛 | 博客
  • 博客访问: 451785
  • 博文数量: 42
  • 博客积分: 1325
  • 博客等级: 中尉
  • 技术积分: 1312
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-13 18:00
个人简介

呵~~~呵~~~

文章分类

全部博文(42)

文章存档

2016年(3)

2015年(1)

2014年(2)

2013年(2)

2012年(7)

2011年(11)

2010年(3)

2009年(13)

我的朋友

分类: LINUX

2009-03-24 18:40:36

c + mysql 简单使用例程。
检查一个表是否存在,不存在则创建。编译时加入 -lmysqlclient
附: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;
}

阅读(1158) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~