MySQL学习笔记_12_Linux下C++/C连接MySQL数据库(二) --返回数据的SQL
2015-05-08 11:13
776 查看
从MySQL数据库值哦功能检索数据有4个步骤:
1)发出查询
2)检索数据
3)处理数据
4)整理所需要的数据
用mysql_query()发出查询,检索数据可以使用mysql_store_result()或mysql_use_result(),取决与怎样检索数据,接着是调用mysql_fetch_row()来处理数据,最后,还必须调用mysql_free_result()以允许MySQL进行必要的整理工作。
view plaincopyprint?
MYSQL_RES *mysql_store_result(MYSQL * connection);
//成功返回结构体指针,失败返回NULL
my_ulonglong mysql_num_row(MYSQL_RES * result);
//减速实际返回的行数
MYSQL_ROW mysql_fetch_row(MYSQL_RES * result);
//从mysql_store_result()中得到结果的结构体,并从中检索单个行,当没有更多的数据,或者出错时,返回NULL
void mysql_free_result(MYSQL_RES * result);
//使mySQL数据库整理分配的对象,关闭连接.
示例:
[cpp]
view plaincopyprint?
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <mysql/mysql.h>
using namespace std;
void mysql_err_function(MYSQL * connection);
int main()
{
MYSQL * connection;
connection = mysql_init(NULL);
if (!connection)
{
mysql_err_function(connection);
}
connection = mysql_real_connect(connection,"localhost","root","123456","test",0,NULL,0);
if (!connection)
{
mysql_err_function(connection);
}
cout << "Connection to MySQL Server is Success..." << endl;
string query;
getline(cin,query);
int res = mysql_query(connection,query.c_str());
if (res)
{
mysql_err_function(connection);
}
MYSQL_RES * my_res = mysql_store_result(connection);
cout << "Retrieved " << mysql_num_rows(my_res) << "rows" << endl;
MYSQL_ROW sqlrow;
while ((sqlrow = mysql_fetch_row(my_res)))
{
cout << "Fetched data..." << endl;
}
mysql_free_result(my_res);
mysql_close(connection);
cout << "Connection to MySQL Server is closed!" << endl;
return 0;
}
void mysql_err_function(MYSQL * connection)
{
if (mysql_errno(connection))
{
cout << "Error " << mysql_errno(connection) << " : "
<< mysql_error(connection) << endl;
exit(-1);
}
}
view plaincopyprint?
MYSQL_RES *mysql_use_result(MYSQL * connection);
//成功返回结果集,失败返回NULL
一次取全部数据增加了网络负载,增加了时延,但是可以保证数据的完整性。
示例:
[cpp]
view plaincopyprint?
#include <iostream>
#include <cstdlib>
#include <mysql/mysql.h>
using namespace std;
void mysql_err_function(MYSQL * connection);
int main()
{
MYSQL * connection;
connection = mysql_init(NULL);
if (mysql_real_connect(connection,"localhost","root","123456","test",0,NULL,0))
{
cout << "Connection to MySQL Server is Succeed..." << endl;
string query;
getline(cin,query);
int res = mysql_query(connection,query.c_str());
if (res)
{
mysql_err_function(connection);//mysql_err_function()实现代码参考上例
}
else
{
MYSQL_RES * my_res = mysql_use_result(connection);
if (my_res)
{
MYSQL_ROW sqlrow;
while ((sqlrow = mysql_fetch_row(my_res)))
{
cout << "Fetching the Data..." << endl;
}
mysql_free_result(my_res);
}
else
{
mysql_err_function(connection);
}
}
mysql_close(connection);
cout << "Connection to MySQL Server is Closed!" << endl;
}
else
{
mysql_err_function(connection);
}
}
Linux下C++/C连接MySQL数据库(二)
--返回数据的SQL
引:
返回数据的SQL是指通过查询语句从数据库中取出满足条件的数据记录从MySQL数据库值哦功能检索数据有4个步骤:
1)发出查询
2)检索数据
3)处理数据
4)整理所需要的数据
用mysql_query()发出查询,检索数据可以使用mysql_store_result()或mysql_use_result(),取决与怎样检索数据,接着是调用mysql_fetch_row()来处理数据,最后,还必须调用mysql_free_result()以允许MySQL进行必要的整理工作。
1、一次提取所有数据
[cpp]view plaincopyprint?
MYSQL_RES *mysql_store_result(MYSQL * connection);
//成功返回结构体指针,失败返回NULL
my_ulonglong mysql_num_row(MYSQL_RES * result);
//减速实际返回的行数
MYSQL_ROW mysql_fetch_row(MYSQL_RES * result);
//从mysql_store_result()中得到结果的结构体,并从中检索单个行,当没有更多的数据,或者出错时,返回NULL
void mysql_free_result(MYSQL_RES * result);
//使mySQL数据库整理分配的对象,关闭连接.
MYSQL_RES *mysql_store_result(MYSQL * connection); //成功返回结构体指针,失败返回NULL my_ulonglong mysql_num_row(MYSQL_RES * result); //减速实际返回的行数 MYSQL_ROW mysql_fetch_row(MYSQL_RES * result); //从mysql_store_result()中得到结果的结构体,并从中检索单个行,当没有更多的数据,或者出错时,返回NULL void mysql_free_result(MYSQL_RES * result); //使mySQL数据库整理分配的对象,关闭连接.
示例:
[cpp]
view plaincopyprint?
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <mysql/mysql.h>
using namespace std;
void mysql_err_function(MYSQL * connection);
int main()
{
MYSQL * connection;
connection = mysql_init(NULL);
if (!connection)
{
mysql_err_function(connection);
}
connection = mysql_real_connect(connection,"localhost","root","123456","test",0,NULL,0);
if (!connection)
{
mysql_err_function(connection);
}
cout << "Connection to MySQL Server is Success..." << endl;
string query;
getline(cin,query);
int res = mysql_query(connection,query.c_str());
if (res)
{
mysql_err_function(connection);
}
MYSQL_RES * my_res = mysql_store_result(connection);
cout << "Retrieved " << mysql_num_rows(my_res) << "rows" << endl;
MYSQL_ROW sqlrow;
while ((sqlrow = mysql_fetch_row(my_res)))
{
cout << "Fetched data..." << endl;
}
mysql_free_result(my_res);
mysql_close(connection);
cout << "Connection to MySQL Server is closed!" << endl;
return 0;
}
void mysql_err_function(MYSQL * connection)
{
if (mysql_errno(connection))
{
cout << "Error " << mysql_errno(connection) << " : "
<< mysql_error(connection) << endl;
exit(-1);
}
}
#include <iostream> #include <fstream> #include <cstdlib> #include <mysql/mysql.h> using namespace std; void mysql_err_function(MYSQL * connection); int main() { MYSQL * connection; connection = mysql_init(NULL); if (!connection) { mysql_err_function(connection); } connection = mysql_real_connect(connection,"localhost","root","123456","test",0,NULL,0); if (!connection) { mysql_err_function(connection); } cout << "Connection to MySQL Server is Success..." << endl; string query; getline(cin,query); int res = mysql_query(connection,query.c_str()); if (res) { mysql_err_function(connection); } MYSQL_RES * my_res = mysql_store_result(connection); cout << "Retrieved " << mysql_num_rows(my_res) << "rows" << endl; MYSQL_ROW sqlrow; while ((sqlrow = mysql_fetch_row(my_res))) { cout << "Fetched data..." << endl; } mysql_free_result(my_res); mysql_close(connection); cout << "Connection to MySQL Server is closed!" << endl; return 0; } void mysql_err_function(MYSQL * connection) { if (mysql_errno(connection)) { cout << "Error " << mysql_errno(connection) << " : " << mysql_error(connection) << endl; exit(-1); } }
2、一次提取一行数据,用于处理了大量的数据集
[cpp]view plaincopyprint?
MYSQL_RES *mysql_use_result(MYSQL * connection);
//成功返回结果集,失败返回NULL
MYSQL_RES *mysql_use_result(MYSQL * connection); //成功返回结果集,失败返回NULL
一次取全部数据增加了网络负载,增加了时延,但是可以保证数据的完整性。
示例:
[cpp]
view plaincopyprint?
#include <iostream>
#include <cstdlib>
#include <mysql/mysql.h>
using namespace std;
void mysql_err_function(MYSQL * connection);
int main()
{
MYSQL * connection;
connection = mysql_init(NULL);
if (mysql_real_connect(connection,"localhost","root","123456","test",0,NULL,0))
{
cout << "Connection to MySQL Server is Succeed..." << endl;
string query;
getline(cin,query);
int res = mysql_query(connection,query.c_str());
if (res)
{
mysql_err_function(connection);//mysql_err_function()实现代码参考上例
}
else
{
MYSQL_RES * my_res = mysql_use_result(connection);
if (my_res)
{
MYSQL_ROW sqlrow;
while ((sqlrow = mysql_fetch_row(my_res)))
{
cout << "Fetching the Data..." << endl;
}
mysql_free_result(my_res);
}
else
{
mysql_err_function(connection);
}
}
mysql_close(connection);
cout << "Connection to MySQL Server is Closed!" << endl;
}
else
{
mysql_err_function(connection);
}
}
相关文章推荐
- MySQL学习笔记_13_Linux下C++/C连接MySQL数据库(三) --处理返回数据
- MySQL学习笔记_11_Linux下C++/C连接MySQL数据库(一)
- Linux学习之CentOS--CentOS6.5下Mysql数据库的安装及C/C++连接使用Mysql
- linux学习第五十篇:MySQL创建用户以及授权,常用sql语句,MySQL数据库备份恢复
- Mysql学习笔记(五)数据查询之测试sql部分。
- Linux学习笔记12——配置ftp、squid、Tomcat、Samba、MySQL主从
- MySQL学习笔记12:数据类型
- MySQL学习笔记2---MySQL数据库的存储引擎和数据类型
- Linux+javaEE的学习笔记之MySQL的安装和备份、回复数据
- php学习笔记(二)php与mysql连接与用php发送SQL查询
- MySQL学习笔记12:数据类型
- MySQL学习笔记12:数据类型
- 马哥学习笔记十四——MySQL进阶之数据类型和sql模型
- c++ 学习笔记(高级linux编程) day12
- [进阶]MySQL学习笔记五数据表连接Join
- 6.(Mysql数据管理相关)连接MYSQL,修改密码,增加新用户,数据库相关命令,表操作相关命令,数据相关命令,数据库sql导入和导出,备份数据库,查看不到mysql数据库的解决办法
- 如何用 ajax 连接mysql数据库,并且获取从中返回的数据。ajax获取从mysql返回的数据。responseXML分别输出不同数据的方法。
- Spring MVC 学习笔记11 —— 后端返回json格式数据
- Unity3D 学习笔记(四) Unity连接MySQL数据库
- Linux netfilter 学习笔记 之七 ip层netfilter的连接跟踪模块的概念及相关的数据结构分析