MySQL 提供的C API ,应用的例子

来源:互联网 发布:美容软件 编辑:程序博客网 时间:2024/05/16 05:17
 转自http://blog.chinaunix.net/u/22617/showart_246749.html
我又研究了一下 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 <winsock2.h>
//
windows平台,假设链接时发现很多链接错误的时候,需要加上对winsock2.h的引用。
#include <stdio.h>
#include <mysql/mysql.h>

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<numcols; c++) {
                        printf("%s/t", row[c]);
                }
                printf("/n");
        }

}