Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1615421
  • 博文数量: 197
  • 博客积分: 10046
  • 博客等级: 上将
  • 技术积分: 1983
  • 用 户 组: 普通用户
  • 注册时间: 2006-08-07 12:36
个人简介

在外企做服务器开发, 目前是项目经理, 管理两个server开发的项目。不做嵌入式好久了。

文章分类
文章存档

2011年(2)

2010年(6)

2009年(18)

2008年(30)

2007年(100)

2006年(41)

分类: LINUX

2007-02-13 10:57:41

【原创】MySQL 提供的C API ,应用的例子

我又研究了一下 MySQL提供的C语言的API 因为上次我提供的页面有创建远程用户  以及 修改mysql用户名和密码的 所以CGI里面必须调用 mysqlC API才能完成。 经过确认, 这些都可以被MySQL C API所支持。


比如 create database create table modify table index 等等。

我们CGI里面所要做的就是 调用MySQL C API 来修改 MySQL 数据库里面的 mysql 数据库里面的user表即可。

下面是个例子

1>
连接MySQL 数据库,并use mysql;
2> select * from user;   //
查询表里面的数据
3>
打印即可

编译:

gcc -o mysql_example mysql_example.c -I/usr/local/include/mysql -L /usr/local/lib/mysql/  -lmysqlclient  

执行结果:
#shell> ./mysql_example

[Copy to clipboard]

CODE:

localhost        root        *E6CC90B878B948C35E92B003C792C46C58C4AF40        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y                                        0        0        0        0       
boblinux        root                Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y                                        0        0        0        0       
boblinux                        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N                                        0        0        0        0       
localhost                        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N        N                                        0        0        0        0       
%        admin        *4ACFE3202A5FF5CF467898FC58AAB1D615029441        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y        Y                                        0        0        0        0       


代码:

[Copy to clipboard]

CODE:

/* 载入相关头文件*/
//#include
//
windows平台,假设链接时发现很多链接错误的时候,需要加上对winsock2.h的引用。
#include
#include

main() {
       
        MYSQL mysql;
        MYSQL_RES *result;
       
        MYSQL_ROW row;
        int numrows, numcols, c;
        char query[] = "SELECT *  FROM user";
       
        mysql_init(&mysql);

        /*
连接到数据库*/

        if (!mysql_real_connect(&mysql, "172.21.5.179",        "admin", "admin",        "mysql", 0, NULL, 0))
        {
                //
访问失败输出错误信息
                fprintf(stderr,        "Failed to connect to database: Error %d:%s\n", mysql_errno(&mysql),mysql_error(&mysql));
                return -1;
        }

        /*
执行一个查询 */

        if (mysql_query(&mysql, query))
        {
                //
查询失败输出错误信息
                fprintf(stderr,        "Error executing query: Error %d: %s\n",                mysql_errno(&mysql), mysql_error(&mysql));
        }

        /*
处理查询结果*/
       
       
//        25.2.3.47. mysql_num_rows()
//        my_ulonglong mysql_num_rows(MYSQL_RES *result)
//       
//       
描述
//       
//       
返回结果集中的行数。
//       
//        mysql_num_rows()
的使用取决于是否采用了mysql_store_result()mysql_use_result()来返回结果集。如果使用了mysql_store_result(),可以立刻调用mysql_num_rows()。如果使用了mysql_use_result()mysql_num_rows()不返回正确的值,直至检索了结果集中的所有行为止。
//       
//       
返回值
//       
//       
结果集中的行数。
        result = mysql_store_result(&mysql);   //mysql_use_result()
mysql_store_result()

        if (!result)
        {
                //
查询结果出错
                fprintf(stderr,        "Error executing query: Error %d: %s\n", mysql_errno(&mysql), mysql_error(&mysql));
        }

        /*
查找查询结果的列数 */

        numcols = mysql_num_fields(result);
        numrows = mysql_num_rows(result);
       
        printf("filds = %d\n",numcols);
        printf("rows = %d\n",numrows);
        printf("\n\n");

        /*
循环显示查询结果 */

        while (row = mysql_fetch_row(result)) {
                for(c=0; c                        printf("%s\t", row[c]);
                }
                printf("\n");
        }

}

 

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