背景

最近一直在看odb官网上odb-manual,由于是全英文文档,仔细看过一遍之后虽然感觉基本了解,但是隔几天之后再翻开时又要逐一回忆当初理解的一些细节,毕竟不是母语,没有那种一看就条件反射式的想起来,所以把一些key word记录下来,避免下次重复花时间来消化知识。对于一个coder,能够用代码来阐述的,就尽量不用文字,所以在记录过程中尽量通过代码来展示一些api的用法。

使用说明

本文基于odb官方手册为指导,通过自己的理解之后用尽可能通俗易懂的文字来阐述相关用法。站在应用编程的角度,将文档中常用知识点、易错点进行翻译整理。同时本文也弱化掉了一些不常用的知识点,基于这种考虑主要是由于本文的立足点是为了使编程人员尽快上手开发,如果将一些不常用的知识点也罗列其中会显得繁杂且冗余,所以如果在开发过程中遇到本文未提及的点可以回到odb官方手册进行查询。

Hello World实例

  1. 声明持久化类
    比如一个person.hxx类的头文件如下所示

    // person.hxx
    //
    #include <string>
    class person
    {public:person(const std::string& first,const std::string& last,unsigned short age);const std::string& first () const;const std::string& last () const;unsigned short age () const;void age (unsigned short);
    private:std::string first_;std::string last_;unsigned short age_;
    };
    

    如果需要存储在数据库中,则需要将person类的声明部分进行改造,需要修改的地方有(1)(2)(3)(4)(5),修改后的声明如下:

    // person.hxx
    //
    #include <odb/core.hxx> // (1)
    #include <string>#pragma db object // (2)
    class person
    {public:person(const std::string& first,const std::string& last,unsigned short age);const std::string& first () const;const std::string& last () const;unsigned short age () const;void age (unsigned short);
    private:person () {} // (3)friend class odb::access; // (4)#pragma db id auto // (5)unsigned long id_; // (5)std::string first_;std::string last_;unsigned short age_;
    };
    
  2. 产生odb的中间文件

    odb -d mysql --generate-query person.hxx
    

    如果需要mysql语句,则执行如下指令:

    odb -d mysql --generate-query --generate-schema person.hxx
    
  3. 编译运行
    如果是首次运行,需要在mysql数据库中将mysql语句文件导入其中,以此来生成数据库相关的表

    mysql --user=odb_test --database=odb_test < person.sql
    
  4. 创建持久化对象

    源码如下:

    // 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{// mysql server's host name: root, password:123456, ip:198.1.17.252, port:30306auto_ptr<database> db(new odb::mysql::database("root", "123456", "odb_test", "198.1.17.252", 30306));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.//15 Revision 2.4, February 2015 C++ Object Persistence with ODB2.4 Making Objects Persistentjohn_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;}
    }
    
  5. 查询数据库对象

    // 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{// mysql server's host name: root, password:123456, ip:198.1.17.252, port:30306auto_ptr<database> db(new odb::mysql::database("root", "123456", "odb_test", "198.1.17.252", 30306));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.//15 Revision 2.4, February 2015 C++ Object Persistence with ODB2.4 Making Objects Persistentjohn_id = db->persist (john);jane_id = db->persist (jane);joe_id = db->persist (joe);t.commit ();}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;}
    }
    
  6. 数据库更新

    // 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{// mysql server's host name: root, password:123456, ip:198.1.17.252, port:30306auto_ptr<database> db(new odb::mysql::database("root", "123456", "odb_test", "198.1.17.252", 30306));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.//15 Revision 2.4, February 2015 C++ Object Persistence with ODB2.4 Making Objects Persistentjohn_id = db->persist (john);jane_id = db->persist (jane);joe_id = db->persist (joe);t.commit ();}// Joe Dirt just had a birthday, so update his age.//{transaction t (db->begin ());auto_ptr<person> joe (db->load<person> (joe_id));joe->age (joe->age () + 1);db->update (*joe);t.commit ();}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;}
    }
    
  7. 删除数据库

    // 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{// mysql server's host name: root, password:123456, ip:198.1.17.252, port:30306auto_ptr<database> db(new odb::mysql::database("root", "123456", "odb_test", "198.1.17.252", 30306));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.//15 Revision 2.4, February 2015 C++ Object Persistence with ODB2.4 Making Objects Persistentjohn_id = db->persist (john);jane_id = db->persist (jane);joe_id = db->persist (joe);t.commit ();}// Joe Dirt just had a birthday, so update his age.//{transaction t (db->begin ());auto_ptr<person> joe (db->load<person> (joe_id));joe->age (joe->age () + 1);db->update (*joe);t.commit ();}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 ();}// John Doe is no longer in our database.//{transaction t (db->begin ());db->erase<person> (john_id);t.commit ();}// John Doe is no longer in our database. An alternative// implementation without using the object id.//{transaction t (db->begin ());// Here we know that there can be only one John Doe in our// database so we use the query_one() shortcut again.//auto_ptr<person> john (db->query_one<person> (query::first == "John" &&query::last == "Doe"));if (john.get () != 0)db->erase (*john);t.commit ();}}catch (const odb::exception& e){cerr << e.what () << endl;return 1;}
    }
    

    上一篇 odb 使用指南(一)环境搭建
    下一篇 odb 使用指南(三)持久化对象的处理

odb 使用指南(二)Hello World相关推荐

  1. odb 使用指南(三)持久化对象的处理

    背景 最近一直在看odb官网上odb-manual,由于是全英文文档,仔细看过一遍之后虽然感觉基本了解,但是隔几天之后再翻开时又要逐一回忆当初理解的一些细节,毕竟不是母语,没有那种一看就条件反射式的想 ...

  2. odb 使用指南(一)环境搭建

    下载安装包 访问odb官方下载,需要下载的文件有以下三个: libodb-2.4.0.tar.gz libodb-mysql-2.4.0.tar.gz odb-2.4.0-1.x86_64.rpm 其 ...

  3. WPF实用指南二:移除窗体的图标

    原文:WPF实用指南二:移除窗体的图标 WPF没有提供任何功能来移除窗体上的icon图标.一般的做法是设置一个空白的图标,如下图1: 这种做法在窗体边框与标题之间仍然会保留一片空白. 比较好的做法是使 ...

  4. linux启动nifi指令,Nifi 组件脚本开发 - ExecuteScript 使用指南 (二)

    Nifi 组件脚本开发 - ExecuteScript 使用指南 (二) 浅谈 Java 的反射原理 摘要: Java 的编译过程 谈及反射, 不得不先了解一下, java 的整个编译过程, 整体的 ...

  5. 会议指南二维码生成_包装和准备技术会议的指南

    会议指南二维码生成 北半球的春天如雨后春笋般涌现,这意味着技术会议季节即将来临. LinuxFest Northwest , OSCON , OpenStack Summit , Write Docs ...

  6. Swift语言指南(二)--语言基础之注释和分号

    Swift语言指南(二)--语言基础之注释和分号 原文:Swift语言指南(二)--语言基础之注释和分号 注释 通过注释向自己的代码中注入不可执行的文本,作为你自己的笔记或提示.Swift编译器运行时 ...

  7. 高通机器视觉快速指南二

    高通机器视觉快速指南 二 3 校准 3.1 一个良好的位置 3.2 追踪相机 3.3 立体相机校准 3.4 高分辨率相机校准 3 校准 尽管设计用于演示可集成到系统中的 MV,但这些应用程序也可用于校 ...

  8. 端粒效应《The Telemere Effect》程序员的养生指南(二)情绪、思维模式与健康

    身为程序员,面临着久坐,工作时间长,工作量大等种种问题.健康显得至关重要.接下来,打算借助一本诺奖得主写的书,来探讨下怎么能够更加健康的做好程序员的工作.端粒效应<The Telomere Ef ...

  9. [ISUX译]iOS 9人机界面指南(二):设计策略

    [ISUX译]iOS 9人机界面指南(二):设计策略 雪糕 2015.11.09 文章索引 2.1 设计原则(Design Principles) 2.1.1 美学完整性(Aesthetic Inte ...

  10. Selenium IDE使用指南二(命令行运行器)

    现在,您可以在任何浏览器上,并行和在Grid上运行所有Selenium IDE测试,而无需编写任何代码. 只需安装Selenium IDE命令行运行程序,获取必要的浏览器驱动程序(如果在本地运行测试) ...

最新文章

  1. 中天亮剑——打击网络风暴侵袭
  2. [原]java开发中遇到的问题及解决方法(持续更新)
  3. 程序世界的秘密(中)
  4. PHP在Postman上面进行xdebug的测试
  5. C++string容器-字符串比较
  6. 不要在给自己不学习找借口了,否则…
  7. 前景检测算法(十七)--基于光流算法
  8. Java程序员须知的七个日志管理工具(转)
  9. html+css京东登录页面
  10. Linux驱动开发-编写W25Q64(Flash)驱动
  11. 史海峰:我的架构师修炼之道
  12. TFS2012 权限设置
  13. Python转换excel文件,将xlsx文件转换为xls文件
  14. AMD:无限你我的无限
  15. 转自汇编网: 高三老师给大一学生的一封信(感动!)
  16. 数字报刊平台php,现代快报多媒体数字报刊平台
  17. 华为机试真题 Python 实现【相同数字的积木游戏】【2022.11 Q4 新题】
  18. maya镜像模型但不改变点序 对称操作
  19. 如何看待侵权行为?有存在的合理性吗?
  20. 四川省粮食生产支持补贴申报对象补助

热门文章

  1. 数据清洗 Chapter01 | 数据清洗概况
  2. 关闭Ubuntu错误报告
  3. linux安装Elasticsearch全文搜索引擎
  4. DM manager工具使用
  5. APK可视化修改工具:APK改之理(APK IDE)
  6. 无线PLC专用数据终端应用方案
  7. ActionForm的详解
  8. android手机双卡的电话录音,苹果与android手机电话通话录音
  9. es6兼容性问题解决
  10. u盘 固态硬盘 读写速度测试软件,超级U盘/SSD读写可靠性(扩容)测试工具urwtest v1.8...