mysql c++
2016-08-08 22:58
281 查看
#include "stdafx.h" #include<iostream> #include <mysql.h> using namespace std; void main() { const char user[] = "root"; //username const char pswd[] = "root"; //password const char host[] = "localhost"; //or"127.0.0.1" const char table[] = "test"; //database unsigned int port = 3306; //server port MYSQL myCont; MYSQL_RES *result; MYSQL_ROW sql_row; MYSQL_FIELD *fd; char column[32][32]; int res; mysql_init(&myCont); if (mysql_real_connect(&myCont, host, user, pswd, table, port, NULL, 0)) { cout << "connect succeed!" << endl; mysql_query(&myCont, "SET NAMES GBK"); //设置编码格式,否则在cmd下无法显示中文 res = mysql_query(&myCont, "select * from userinfo");//查询useinfo我建的表 if (!res) { result = mysql_store_result(&myCont);//保存查询到的数据到result if (result) { int i, j; cout << "number of result: " << (unsigned long)mysql_num_rows(result) << endl; for (i = 0; fd = mysql_fetch_field(result); i++)//获取列名 { strcpy(column[i], fd->name); } j = mysql_num_fields(result); for (i = 0; i<j; i++) { printf("%s\t", column[i]); } printf("\n"); while (sql_row = mysql_fetch_row(result))//获取具体的数据 { for (i = 0; i<j; i++) { printf("%s\t", sql_row[i]); } printf("\n"); } } } else { cout << "query sql failed!" << endl; } } else { cout << "connect failed!" << endl; } if (result != NULL) mysql_free_result(result);//释放结果资源 mysql_close(&myCont);//断开连接 }//设置64位平台数据库要和软件位数一样//mysql.dll和exe同目录//mysql.h,mysql.lib要包含
转载:http://www.cnblogs.com/lovebread/archive/2009/11/24/1609936.html
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C++的template模板中class与typename关键字的区别分析
- C与C++之间相互调用实例方法讲解