MySQL 提供的C API ,应用的例子
来源:互联网 发布:美容软件 编辑:程序博客网 时间:2024/05/16 05:17
转自http://blog.chinaunix.net/u/22617/showart_246749.html
我又研究了一下 MySQL提供的C语言的API ,因为上次我提供的页面有创建远程用户 以及修改mysql用户名和密码的项,所以CGI里面必须调用 mysql的C 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
比如 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
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");
}
}
//#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");
}
}
- MySQL 提供的C API ,应用的例子
- 通过MySQL提供的C API访问MySQL数据库
- 通过MySQL C API界面(mysql自己提供的API方式访问mysql数据库)
- 利用MYSQL提供的API操作MYSQL数据库(C语言)
- mysql提供给c语言的api 和c语言连接mysql例子程序
- MYSQL C API的简单应用 windows下
- API写的ServerSocket多线程应用例子
- 利用百度地图提供的API做的应用
- Zookeeper提供的API
- Redis提供的API
- 为 UWP 应用提供的 .NET 网络 API
- 一个REST API 在Azure上应用的简单通讯录例子。(C#, MVC)
- 详细的MySQL C API
- 详细的MySQL C API
- mysql c api的使用
- mysql C API的使用
- MySQL的C语言API
- TI提供的例子简介
- 转:C++指针直接调用类成员函数探讨
- DE Decompiler Lite 1.0 by GPcH
- Inno Setup 5.2.2 released
- 查看SQL Server 补丁的版本
- 转:C++模板元编程
- MySQL 提供的C API ,应用的例子
- 2007-10-24下厨记
- 提供可靠的内容下载以提升用户体验
- 使用ResultSetMetaData显示字段的名字等信息
- 转:VC++对象模型
- VC++中对于处理消息的学习总结
- TRUNCATE和DELETE的几点区别
- 转:Boost源码剖析之:容器赋值-assign
- 转:C++多态技术