查询的机制:
(1)查询是通过查询模板类型odb::query和查询结果模板类型odb::result来共同完成的
(2)查询类型odb::query提供bool表达式来存储查询条件,
(3)查询结果类型odb::result用来存储结果,并提供迭代器给客户访问查询结果
(4)db对象调用自己的query方法来执行查询,并将查询方法的参数设置为查询类型odb::query的静态成员组合而成的bool表达式
查询person表中年龄大于30岁的人,并简单的打印这些人的信息:
源代码:
// driver.cxx
//
#include <memory> // std::auto_ptr
#include <iostream>
#include <odb/database.hxx>
#include <odb/transaction.hxx>
#include <odb/mysql/database.hxx>
#include "person.hxx"
#include "person-odb.hxx"
using namespace std;
using namespace odb::core;
int
main (int argc, char* argv[])
{
try
{
//auto_ptr<database> db (new odb::mysql::database (argc, argv));
//连接数据库
auto_ptr<odb::database> db (
new odb::mysql::database (
"root" // database login name
,"123456" // database password
,"collect" // database name
,"localhost"
,13306
));
typedef odb::query<person> query;
typedef odb::result<person> result;
// Say hello to those over 30.
//
{
transaction t (db->begin ());
result r (db->query<person> (query::age > 30));
for (result::iterator i (r.begin ()); i != r.end (); ++i)
{
cout << "Hello, " << i->first () << "!" << endl;
}
t.commit ();
}
}
catch (const odb::exception& e)
{
cerr << e.what () << endl;
return 1;
}
}
person里面的数据:
执行结果:
原文地址:
http://www.codesynthesis.com/products/odb/doc/manual.xhtml#2.5