MySQL++简介

Mysql++是官方发布的、一个为MySQL设计的C++语言的API。Mysql++为Mysql的C-Api的再次封装,它用STL(Standard Template Language)开发并编写,并为C++开发者提供像操作STL容器一样方便的操作数据库的一套机制。其官方API介绍:MySQL++ Reference Manual

与JDBC一样,先建立连接Connection,创建Query,执行操作获得结果。

SimpleResult Query::store()进行更新、删除、创建等操作,SimpleResult代表执行状态,查询是否成功、影响了多少行。

StoreQueryResult Query::store():最常用。StoreQueryResult继承vector<mysqlpp::Row>,Row类似于vector<string>,表示一行中各列的数据。使用可以result[1][4]或result[2]["price"]。

UseQueryResult Query::use(),大结果查询,UserQueryResult是iterator,一次只能获得一个行结果,不停地next直到结束为止。

简单示例

 

#include <mysql++.h>
// Connect to the sample database.
mysqlpp::Connection conn(false);
if (conn.connect(db, server, user, pass)) {
    // Retrieve a subset of the sample stock table set up by resetdb
    // and display it.
    mysqlpp::Query query = conn.query("select item from stock");
    if (mysqlpp::StoreQueryResult res = query.store()) {
       cout << "We have:" << endl;
       for (size_t i = 0; i < res.num_rows(); ++i) {
            cout << '\t' << res[i][0] << endl;
         }
     }
     else {
        cerr << "Failed to get item list: " << query.error() << endl;
        return 1;
     }
     return 0;
}
else {
    cerr << "DB connection failed: " << conn.error() << endl;
    return 1;
}


对查询结果的处理

 

 

#include <boost/lexical_cast.hpp>
#include <mysql++.h>
lexical_cast<long> ((mysqlpp::Null<string, mysqlpp::NullIsZero>)field)
lexical_cast<int> ((mysqlpp::Null<string, mysqlpp::NullIsZero>)field)
lexical_cast<short> ((mysqlpp::Null<string, mysqlpp::NullIsZero>)field)
lexical_cast<double> ((mysqlpp::Null<string, mysqlpp::NullIsZero>)field)
lexical_cast<std::string> ((mysqlpp::Null<string, mysqlpp::NullIsBlank>)field)


 

转载于:https://www.cnblogs.com/whuqin/archive/2012/07/18/4982043.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值