调研
- awesome cpp的database列表
- Hiberlite ORM仅支持sqlite
- [ODB]简洁,支持主流数据库,官方手册
- OTL不支持ORM,例子在这里
- SOCI同样不支持ORM,查看例子
下载
- odb-2.4.0-i686-windows
- libodb-2.4.0
- libodb-sqlite-2.4.0(自行选择)
- libodb-boost-2.4.0(自行选择,主要为智能指针添加支持)
编译
- 添加odb的环境变量:
C:\Users\Administrator
λ odb --version
ODB object-relational mapping (ORM) compiler for C++ 2.4.0
Copyright (c) 2009-2015 Code Synthesis Tools CC
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- 编译libodb-vc10.sln
编译libodb-sqlite-vc10.sln
注意:- 需要添加包含目录
需要包含链接库目录,由于sqlite本身没有提供lib文件,选择使用vs自动生成
E:\DB\sqlite>LIB /DEF:sqlite3.def /machine:IX86 Microsoft (R) Library Manager Version 10.00.30319.01 Copyright (C) Microsoft Corporation. All rights reserved. 正在创建库 sqlite3.lib 和对象 sqlite3.exp
右键属性中,取消预处理器中的的SQLITE_ENABLE_UNLOCK_NOTIFY命令,然后可以正常编译工程;否则将出现下面的问题
1> 正在创建库 ..\..\lib\odb-sqlite-d.lib 和对象 ..\..\lib\odb-sqlite-d.exp 1>connection.obj : error LNK2019: 无法解析的外部符号 _sqlite3_unlock_notify,该符号在函数 "public: void __thiscall odb::sqlite::connection::wait(void)" (?wait@connection@sqlite@odb@@QAEXXZ) 中被引用 1>E:\DB\libodb-sqlite-2.4.0\odb\sqlite\..\..\bin\odb-sqlite-d-2.4-vc10.dll : fatal error LNK1120: 1 个无法解析的外部命令 1> 1>生成失败。
示例
编译example项目的access-sqlite-vc10工程
#ifndef PERSON_HXX
#define PERSON_HXX
#include <string>
#include <odb/core.hxx>
#pragma db object
class person
{
public:
person () {}
person (const std::string& email,
const std::string& first,
const std::string& middle,
const std::string& last,
unsigned short age)
: email_ (email), first_ (first), middle_ (middle), last_ (last)
{
data_.age = age;
}
// 标准存取器和修改器能够被odb发现使用
const std::string& email () const
{
return email_;
}
void email (const std::string& email)
{
email_ = email;
}
const std::string& getFirst () const
{
return first_;
}
std::string& setFirst ()
{
return first_;
}
const std::string& g_middle () const
{
return middle_;
}
void s_middle (const std::string& middle)
{
middle_ = middle;
}
const char* last () const
{
return last_.c_str ();
}
void last (const char* last)
{
last_ = last;
}
unsigned short age () const
{
return data_.age;
}
void age (unsigned short age)
{
data_.age = age;
}
private:
#pragma db id //自动生成主键
std::string email_;
std::string first_;
#pragma db get(g_middle) set(s_middle)
std::string middle_;
#pragma db get(std::string (this.last ())) set(last ((?).c_str ()))
std::string last_;
#pragma db transient
struct
{
unsigned short age;
} data_;
#pragma db member(age) virtual(unsigned short)
};
#endif // PERSON_HXX
main函数
#include <memory> // std::auto_ptr
#include <iostream>
#include <odb/database.hxx>
#include <odb/transaction.hxx>
#include "database.hxx" // create_database
#include "person.hxx"
#include "person-odb.hxx"
using namespace std;
using namespace odb::core;
int
main (int argc, char* argv[])
{
try
{//创建db
auto_ptr<database> db (create_database (argc, argv));
{
person john ("john@doe.com", "John", "X", "Doe", 31);
person jane ("jane@doe.com", "Jane", "Y", "Doe", 29);
transaction t (db->begin ());
db->persist (john);//插入
db->persist (jane);
t.commit ();
}
{//查询
typedef odb::result<person> result;
transaction t (db->begin ());
result r (db->query<person> ());
for (result::iterator i (r.begin ()); i != r.end (); ++i)
cout << i->getFirst () << ' '
<< i->g_middle () << ' '
<< i->last () << ' '
<< i->email () << ' '
<< i->age () << endl;
t.commit ();
}
}
catch (const odb::exception& e)
{
cerr << e.what () << endl;
return 1;
}
}
vs会自动执行odb命令,来生成对应的person-odb.ixx,person-odb.hxx,person-odb.cxx;如果不能成功调用odb,可以参见4。
参考:
[1].http://www.sqlite.org/howtocompile.html
[2].http://blog.csdn.net/feng______/article/details/33411119
[3].http://www.codesynthesis.com/products/odb/doc/manual.xhtml
[4].http://wiki.codesynthesis.com/Using_ODB_with_Microsoft_Visual_Studio