如何使用ODB(How to use odb On windows)
2013-07-10 15:45
465 查看
1.下载ODB library:ODB Compiler,Common Runtime Library,Database Runtime Library。http://www.codesynthesis.com/products/odb/download.xhtml(注意:ODB Compiler为odb-x.x.x-i686-windows, Database Runtime Libraries为你想要用的数据库对应的库)2.解压,安装 你可以在根目录执行odb,也可以加到环境变量里。 3.头文件中定义类,上边为正常类,下边为修改为odb的persistent 类
class person { ... private: string email_; string name_; unsigned short age_; };
#include <string>
#include <odb/core.hxx>
#pragma db object class person { ... private: friend class odb::access; person () {} #pragma db id string email_; string name_; unsigned short age_; };4.利用odb产生数据库支持代码,只有这样子才能利用上面定义的persistent类进行查询。具体命令行参数:http://www.codesynthesis.com/products/odb/doc/odb.xhtml如:odb --database mysql --generate-query D:\MCSF_Backup\MAIN\Features\McsfProtocolDBWrapper\McsfProtocolDBWrapper\include\catalog.hxx生成5.添加生成的3个文件到工程里,使用。 如Insert:
#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)); unsigned long john_id, jane_id, joe_id; // Create a few persistent person objects. // { person john ("John", "Doe", 33); person jane ("Jane", "Doe", 32); person joe ("Joe", "Dirt", 30); transaction t (db->begin ()); // Make objects persistent and save their ids for later use. // john_id = db->persist (john); jane_id = db->persist (jane); joe_id = db->persist (joe); t.commit (); } } catch (const odb::exception& e) { cerr << e.what () << endl; return 1; } }6.注意:在定义的persistent 类中, 有几个#pragma经常使用。1)指定字段为主键: #pragma db id2) 指定字段为数据库相应类型:#pragma db type("VARCHAR(64) binary not null")3)指定字段为数据库相应的列:#pragma db column("CatalogName")4)指定数据库的主键自增长: #pragma db id auto
在用odb产生数据库支持代码时,有时候persistent类中引用了别的库, 在编译时可以通过-I添加路径。
比如:
相关文章推荐
- 如何使用ODB(How to use odb On windows)
- 如何使用ODB(How to use odb On windows)
- How to use the windows active directory to authenticate user via logon form 如何自定义权限系统,使用 active directory验证用户登录
- How to use FTP via Windows Explorer(如何使用资源管理器访问 ftp)
- How to use toolbar control on dialog using CToolBar class in VC++ - VC++ 中用CToolBar 类如何使用ToolBar控件
- 如何使用spy ++ (How to use Spy ++)
- [转载]如何使用SocketAsyncEventArgs类(How to use the SocketAsyncEventArgs class)
- How to use JPA from a JBoss Web application ?(jboss 中如何使用线程安全的EntityManagerFactory )
- How to use bitBucket with EGit in Eclipse,如何在Eclipse中使用bitbucket远程仓库
- 译文:如何使用SocketAsyncEventArgs类(How to use the SocketAsyncEventArgs class)
- 如何使用spy ++ (How to use Spy ++)
- 如何在QT for S60 中使用pthread库(Carbide.c++) (How to use pthread in QT4.6 for S60)
- [转帖]译文:如何使用SocketAsyncEventArgs类(How to use the SocketAsyncEventArgs class)
- 如何使用表维护工具。how to use the table extent maintenance tool
- 译文:如何使用SocketAsyncEventArgs类(How to use the SocketAsyncEventArgs class) (转)
- How to install Ruby on rails in Windows 2003/xp 如何安装ROR
- How to use write and run MapReduce in eclipse on windows.
- 如何使用代码美化器Uncrustify (How to use code beautifier Uncrustify)
- 为何以及如何使用Netlink Socket ( Why and How to Use Netlink Socket)
- [pm] How to setup svn server on windows - 如何在windows上建设svn服务器