目录

一、ODB简介

二、Person类

1、Person.h

2、Person.cpp

三、工程修改

1、Person类修改

2、运行odb

3、添加生成的文件到工程中

4、配置​

5、添加database.hxx文件到工程中

6、添加DATABASE_SQLITE宏

7、main函数

8、copy dll

9、运行


一、ODB简介

ODB是应用于C++的一个开源、跨平台、跨数据库的对象关系映射(ORM)系统。它可以让你持久化C++对象到关系数据库,而不必处理表、列或者SQL,无需手动编写任何映射代码。ODB支持MySQL,SQLite,PostgreSQL,Oracle和微软SQL Server关系数据库以及C ++98/03和C ++11语言标准。它还配备了用于Boost和Qt可选的配置文件,让你可以无缝地使用这些库持久化C++类的值类型、容器和智能指针。它有易用性,简洁的代码,安全,数据库可移植性,优良的性能,可维护性等优点。

ODB不是框架。 它并没有规定您应该如何编写应用程序。 相反,它仅通过处理C ++对象的持久性而不干扰任何其他功能而设计为适合您的样式和体系结构。只需进行少量修改就可以使现有类持久化。 特别是,可以在没有默认构造函数的情况下声明持久类,可以自动使用现有的访问器和修饰符函数来访问数据成员,并且可以将ODB编译指示移出该类并移到单独的头中,从而使对象关系 映射完全是非侵入性的。 对自动数据库模式演变的支持还使您可以像对待应用程序中的任何其他C ++类一样对待ODB持久对象。

二、Person类

1、Person.h

#ifndef PERSON_H
#define PERSON_H
#include <string>
class person
{
public:person(void);~person(void);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 age);private:unsigned long id_;std::string first_;std::string last_;unsigned short age_;};
#endif

2、Person.cpp

#include "StdAfx.h"
#include "person.h"person::person(void)
{
}person::person(const std::string& first,const std::string& last,unsigned short age): first_(first), last_(last), age_(age)
{
}person::~person(void)
{
}const std::string& person::first() const
{ return first_; }const std::string& person::last() const
{ return last_; }unsigned short person::age() const
{ return age_; }void person::age(unsigned short age)
{ age_ = age; }

三、工程修改

1、Person类修改

(1)Person.hxx

将person.h修改为person.hxx

#ifndef PERSON_CXX_H
#define PERSON_CXX_H
#include <string>
#include <odb/core.hxx>           // 包含odb::access
#pragma db object               // 告诉编译器这是一个 persistent class
class person
{
public:person(void);~person(void);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 age);private:friend class odb::access;#pragma db id autounsigned long id_;std::string first_;std::string last_;unsigned short age_;};
#endif

(2)Person.cpp修改

#include "StdAfx.h"
#include "person.hxx"person::person(void)
{
}person::person(const std::string& first,const std::string& last,unsigned short age): first_(first), last_(last), age_(age)
{
}person::~person(void)
{
}const std::string& person::first() const
{ return first_; }const std::string& person::last() const
{ return last_; }unsigned short person::age() const
{ return age_; }void person::age(unsigned short age)
{ age_ = age; }

2、运行odb

odb -d sqlite --generate-query --generate-schema person.hxx

3、添加生成的文件到工程中

将生成的person-odb.hxx,person-odb.cxx和person-odb.ixx添加到项目中

4、配置

 

5、添加database.hxx文件到工程中

// file      : hello/database.hxx
// copyright : not copyrighted - public domain//
// Create concrete database instance based on the DATABASE_* macros.
//#ifndef DATABASE_HXX
#define DATABASE_HXX#include <string>
#include <memory>   // std::auto_ptr
#include <cstdlib>  // std::exit
#include <iostream>#include <odb/database.hxx>#if defined(DATABASE_MYSQL)
#  include <odb/mysql/database.hxx>
#elif defined(DATABASE_SQLITE)
#  include <odb/connection.hxx>
#  include <odb/transaction.hxx>
#  include <odb/schema-catalog.hxx>
#  include <odb/sqlite/database.hxx>
#elif defined(DATABASE_PGSQL)
#  include <odb/pgsql/database.hxx>
#elif defined(DATABASE_ORACLE)
#  include <odb/oracle/database.hxx>
#elif defined(DATABASE_MSSQL)
#  include <odb/mssql/database.hxx>
#else
#  error unknown database; did you forget to define the DATABASE_* macros?
#endifinline std::auto_ptr<odb::database>
create_database (int& argc, char* argv[])
{using namespace std;using namespace odb::core;if (argc > 1 && argv[1] == string ("--help")){cout << "Usage: " << argv[0] << " [options]" << endl<< "Options:" << endl;#if defined(DATABASE_MYSQL)odb::mysql::database::print_usage (cout);
#elif defined(DATABASE_SQLITE)odb::sqlite::database::print_usage (cout);
#elif defined(DATABASE_PGSQL)odb::pgsql::database::print_usage (cout);
#elif defined(DATABASE_ORACLE)odb::oracle::database::print_usage (cout);
#elif defined(DATABASE_MSSQL)odb::mssql::database::print_usage (cout);
#endifexit (0);}#if defined(DATABASE_MYSQL)auto_ptr<database> db (new odb::mysql::database (argc, argv));
#elif defined(DATABASE_SQLITE)auto_ptr<database> db (new odb::sqlite::database (argc, argv, false, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE));// Create the database schema. Due to bugs in SQLite foreign key// support for DDL statements, we need to temporarily disable// foreign keys.//{connection_ptr c (db->connection ());c->execute ("PRAGMA foreign_keys=OFF");transaction t (c->begin ());schema_catalog::create_schema (*db);t.commit ();c->execute ("PRAGMA foreign_keys=ON");}
#elif defined(DATABASE_PGSQL)auto_ptr<database> db (new odb::pgsql::database (argc, argv));
#elif defined(DATABASE_ORACLE)auto_ptr<database> db (new odb::oracle::database (argc, argv));
#elif defined(DATABASE_MSSQL)auto_ptr<database> db (new odb::mssql::database (argc, argv));
#endifreturn db;
}#endif // DATABASE_HXX

6、添加DATABASE_SQLITE宏

7、main函数

#include "stdafx.h"
#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{auto_ptr<database> db (create_database (argc, argv));unsigned long john_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);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 () << " " << i->last () << "!" << endl;}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 ();}// John Doe is no longer in our database.//{transaction t (db->begin ());db->erase<person> (john_id);t.commit ();}}catch (const odb::exception& e){cerr << e.what () << endl;return 1;}
}

(1)存储

  // 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);db->persist (jane);joe_id = db->persist (joe);t.commit ();}

(2)查询

  // 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 () << " " << i->last () << "!" << endl;}t.commit ();}

(3)更新

    // 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 ();}

(4)删除

// John Doe is no longer in our database.//{transaction t (db->begin ());db->erase<person> (john_id);t.commit ();}

8、copy dll

copy odb-d-2.4-vc10.dll,odb-sqlite-d-2.4-vc10.dll和sqlite3.dll到debug目录下

9、运行

跨平台数据库ODB实战3-Person类的存储、查询、更新和删除相关推荐

  1. 跨平台数据库ODB实战2-运行Hello Example

    目录 一.ODB简介 二.Hello Example 1.打开examples-sqlite-vc10.sln 2.设置项目 3.运行项目 4.指定数据库运行程序 5.使用sqlite3.exe查询数 ...

  2. Oracle数据库----表中数据的操作(插入、更新、删除数据)

    文章目录 一.插入数据 插入多行数据的简单方法: 1.将表一的全部数据添加到表二中 创建副本Courses1,只复制Courses的结构 将Courses表中的数据拷贝到Courses1中 创建副本的 ...

  3. Oracle XTTS跨平台数据库迁移(从Unix迁移数据库到Linux)_Oracle数据库迁移项

    Oracle数据库迁移教程04 Oracle XTTS跨平台数据库迁移实战 (真正超越OCP/OCM的项目实战系列教程) 本套风哥Oracle教程学习内容 1.Oracle XTTS技术基础知识 2. ...

  4. Springboot单元测试mysql_Springboot Mybatis-Plus数据库单元测试实战(三种方式)

    单元测试长久以来是热门话题,本文不会讨论需不需要写单测,可以看看参考资料1,我个人认为写好单测应该是每个优秀开发者必备的技能,关于写单测的好处在这里我就不展开讨论了,快速进入本文着重讨论的话题,如何写 ...

  5. Net Core 6.0 webApi+sqlServer数据库教程实战

    Net Core 6.0 webApi+sqlServer数据库教程实战 教程前言 安装net core 环境 构建wepApi项目 操作数据库 接口调用 相关快速扩展 总结 教程前言 本教程从构建项 ...

  6. oledb vc访问mdb数据库_vc实战oledb编程

    VC 中用 ADO 和 DataGrid 控件显示和更新数据库中的数据分类: VC 编程 2010-07-06 15:58 1157 人阅读 评论(0) 收藏 举报 VC 中用 ADO 和 DataG ...

  7. Android 数据库开发实战(简单易懂+DEMO)

    Android数据库开发实战 我们在Android开发中,如果需要本地持久化数据,可以采用SP.数据库.本地文件等方式.SP适合小数据的存取.数据库查询修改方面非常适合.文件存储适合大数据文件的操作. ...

  8. 高颜值数据库项目实战MySQL+JavaFX+Fxml+CSS(完整精讲解版+源代码)(六)

    6:高颜值JavaFX数据库-九讲之六 项目效果图 功能点实现 内部优化 类结构示例 创建数据库连接类 1.创建类 2.数据库连接步骤 3.一百个注意事项 4.类的具体实现 5.错误显示的类 后记 高 ...

  9. 金仓数据库 KingbaseES V8.3 至 V8.6 迁移最佳实践(4. V8.3 到 V8.6 数据库移植实战)

    4. V8.3 到 V8.6 数据库移植实战 由于 KingbaseES 内部兼容特性,在实际应用中,一般只需很少甚至不做任何修改,用户便可把 V8.3 数据库移植到 V8.6 环境中运行.不仅如此, ...

  10. 金仓数据库 MySQL 至 KingbaseES 迁移最佳实践(3. MySQL 数据库移植实战)

    3. MySQL 数据库移植实战 由于 KingbaseES 利用 KDTS-PLUS 等多种工具简化移植过程. 本节重点描述了在实际应用中移植一个 MySQL 数据库系统的完整过程,以及其中的主要移 ...

最新文章

  1. 编程笔试(解析及代码实现):求出一个整数中各位数上所包含全部质数之和
  2. linux几个常用的环境变量配置文件
  3. 交替方向乘子算法(ADMM)
  4. winfrom水晶报表的创建
  5. 为什么要使用spring IOC
  6. 未能加载文件或程序集“Poderosa.Core
  7. 大牛用SSM框架实现了支付宝的支付功能,满满干货指导
  8. 游戏开发中的数据表示
  9. AI Hero 算法挑战赛,万元奖金等你来拿!
  10. Linux的安装及忘记Linux密码的措施
  11. vba窗体 点击增加减少_EXCEL之VBA-窗体实例多页控件的基础应用
  12. logback.xml日志配置文件,springboot
  13. 三列自适应布局(圣杯布局)
  14. 验证码识别登录:使用超级鹰(验证码识别第三方包)识别超级鹰网站登录
  15. Huginn实现自动通过slack推送豆瓣高分电影
  16. w10桌面计算机图标箭头去除,win10专业版电脑桌面图标箭头快速去掉技巧
  17. Sliced Sprite
  18. android 圆形背景文字,android圆形图片,圆形背景文字的CircleTextImageView开源组件
  19. 辉芒微单片机的c语言仿真器,辉芒微单片机
  20. 域名防封之长城防封系统都能做什么?

热门文章

  1. 用C#制作RPG游戏
  2. 【刨根问底】解决我的世界启动,报错openGL版本不足的问题
  3. Microsoft JET Database Engine 错误 '80004005' 操作必须使用一个可更新的查询。问题解决办法
  4. 阿帕奇服务器配置站点,Apache的基本服务器配置
  5. 销售管理软件系统的两大优势是什么?
  6. 单片机与触摸屏通信c语言,讲述如何实现单片机与触摸屏的通信
  7. 如何成为优秀的技术主管-管理篇
  8. 同义词词林 使用 java_利用同义词林计算词的相似度——基于路径与深度的同义词词林词语相似度计算...
  9. 找到某个关键字 同义词词林 python_python基础——标识符
  10. java 日历选择天_如何从Java中的日历对象构建天,月,年的列表?