• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

跨平台数据库ODB3-Person类的存储、查询、更新和删除

武飞扬头像
kupeThinkPoem
帮助1

目录

一、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

  1.  
    #ifndef PERSON_H
  2.  
    #define PERSON_H
  3.  
    #include <string>
  4.  
    class person
  5.  
    {
  6.  
    public:
  7.  
    person(void);
  8.  
    ~person(void);
  9.  
    person(const std::string& first,
  10.  
    const std::string& last,
  11.  
    unsigned short age);
  12.  
     
  13.  
    const std::string& first() const;
  14.  
     
  15.  
    const std::string& last() const;
  16.  
     
  17.  
    unsigned short age() const;
  18.  
     
  19.  
    void age(unsigned short age);
  20.  
     
  21.  
    private:
  22.  
     
  23.  
    unsigned long id_;
  24.  
    std::string first_;
  25.  
    std::string last_;
  26.  
    unsigned short age_;
  27.  
     
  28.  
    };
  29.  
    #endif

2、Person.cpp

  1.  
    #include "StdAfx.h"
  2.  
    #include "person.h"
  3.  
     
  4.  
     
  5.  
    person::person(void)
  6.  
    {
  7.  
    }
  8.  
     
  9.  
    person::person(const std::string& first,
  10.  
    const std::string& last,
  11.  
    unsigned short age)
  12.  
    : first_(first), last_(last), age_(age)
  13.  
    {
  14.  
    }
  15.  
     
  16.  
    person::~person(void)
  17.  
    {
  18.  
    }
  19.  
     
  20.  
    const std::string& person::first() const
  21.  
    { return first_; }
  22.  
     
  23.  
    const std::string& person::last() const
  24.  
    { return last_; }
  25.  
     
  26.  
    unsigned short person::age() const
  27.  
    { return age_; }
  28.  
     
  29.  
    void person::age(unsigned short age)
  30.  
    { age_ = age; }

三、工程修改

1、Person类修改

(1)Person.hxx

         将person.h修改为person.hxx

  1.  
    #ifndef PERSON_CXX_H
  2.  
    #define PERSON_CXX_H
  3.  
    #include <string>
  4.  
    #include <odb/core.hxx> // 包含odb::access
  5.  
    #pragma db object // 告诉编译器这是一个 persistent class
  6.  
    class person
  7.  
    {
  8.  
    public:
  9.  
    person(void);
  10.  
    ~person(void);
  11.  
    person(const std::string& first,
  12.  
    const std::string& last,
  13.  
    unsigned short age);
  14.  
     
  15.  
    const std::string& first() const;
  16.  
     
  17.  
    const std::string& last() const;
  18.  
     
  19.  
    unsigned short age() const;
  20.  
     
  21.  
    void age(unsigned short age);
  22.  
     
  23.  
    private:
  24.  
    friend class odb::access;
  25.  
    #pragma db id auto
  26.  
    unsigned long id_;
  27.  
    std::string first_;
  28.  
    std::string last_;
  29.  
    unsigned short age_;
  30.  
     
  31.  
    };
  32.  
    #endif

(2)Person.cpp修改

  1.  
    #include "StdAfx.h"
  2.  
    #include "person.hxx"
  3.  
     
  4.  
     
  5.  
    person::person(void)
  6.  
    {
  7.  
    }
  8.  
     
  9.  
    person::person(const std::string& first,
  10.  
    const std::string& last,
  11.  
    unsigned short age)
  12.  
    : first_(first), last_(last), age_(age)
  13.  
    {
  14.  
    }
  15.  
     
  16.  
    person::~person(void)
  17.  
    {
  18.  
    }
  19.  
     
  20.  
    const std::string& person::first() const
  21.  
    { return first_; }
  22.  
     
  23.  
    const std::string& person::last() const
  24.  
    { return last_; }
  25.  
     
  26.  
    unsigned short person::age() const
  27.  
    { return age_; }
  28.  
     
  29.  
    void person::age(unsigned short age)
  30.  
    { 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文件到工程中

  1.  
    // file : hello/database.hxx
  2.  
    // copyright : not copyrighted - public domain
  3.  
     
  4.  
    //
  5.  
    // Create concrete database instance based on the DATABASE_* macros.
  6.  
    //
  7.  
     
  8.  
    #ifndef DATABASE_HXX
  9.  
    #define DATABASE_HXX
  10.  
     
  11.  
    #include <string>
  12.  
    #include <memory> // std::auto_ptr
  13.  
    #include <cstdlib> // std::exit
  14.  
    #include <iostream>
  15.  
     
  16.  
    #include <odb/database.hxx>
  17.  
     
  18.  
    #if defined(DATABASE_MYSQL)
  19.  
    # include <odb/mysql/database.hxx>
  20.  
    #elif defined(DATABASE_SQLITE)
  21.  
    # include <odb/connection.hxx>
  22.  
    # include <odb/transaction.hxx>
  23.  
    # include <odb/schema-catalog.hxx>
  24.  
    # include <odb/sqlite/database.hxx>
  25.  
    #elif defined(DATABASE_PGSQL)
  26.  
    # include <odb/pgsql/database.hxx>
  27.  
    #elif defined(DATABASE_ORACLE)
  28.  
    # include <odb/oracle/database.hxx>
  29.  
    #elif defined(DATABASE_MSSQL)
  30.  
    # include <odb/mssql/database.hxx>
  31.  
    #else
  32.  
    # error unknown database; did you forget to define the DATABASE_* macros?
  33.  
    #endif
  34.  
     
  35.  
    inline std::auto_ptr<odb::database>
  36.  
    create_database (int& argc, char* argv[])
  37.  
    {
  38.  
    using namespace std;
  39.  
    using namespace odb::core;
  40.  
     
  41.  
    if (argc > 1 && argv[1] == string ("--help"))
  42.  
    {
  43.  
    cout << "Usage: " << argv[0] << " [options]" << endl
  44.  
    << "Options:" << endl;
  45.  
     
  46.  
    #if defined(DATABASE_MYSQL)
  47.  
    odb::mysql::database::print_usage (cout);
  48.  
    #elif defined(DATABASE_SQLITE)
  49.  
    odb::sqlite::database::print_usage (cout);
  50.  
    #elif defined(DATABASE_PGSQL)
  51.  
    odb::pgsql::database::print_usage (cout);
  52.  
    #elif defined(DATABASE_ORACLE)
  53.  
    odb::oracle::database::print_usage (cout);
  54.  
    #elif defined(DATABASE_MSSQL)
  55.  
    odb::mssql::database::print_usage (cout);
  56.  
    #endif
  57.  
     
  58.  
    exit (0);
  59.  
    }
  60.  
     
  61.  
    #if defined(DATABASE_MYSQL)
  62.  
    auto_ptr<database> db (new odb::mysql::database (argc, argv));
  63.  
    #elif defined(DATABASE_SQLITE)
  64.  
    auto_ptr<database> db (
  65.  
    new odb::sqlite::database (
  66.  
    argc, argv, false, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE));
  67.  
     
  68.  
    // Create the database schema. Due to bugs in SQLite foreign key
  69.  
    // support for DDL statements, we need to temporarily disable
  70.  
    // foreign keys.
  71.  
    //
  72.  
    {
  73.  
    connection_ptr c (db->connection ());
  74.  
     
  75.  
    c->execute ("PRAGMA foreign_keys=OFF");
  76.  
     
  77.  
    transaction t (c->begin ());
  78.  
    schema_catalog::create_schema (*db);
  79.  
    t.commit ();
  80.  
     
  81.  
    c->execute ("PRAGMA foreign_keys=ON");
  82.  
    }
  83.  
    #elif defined(DATABASE_PGSQL)
  84.  
    auto_ptr<database> db (new odb::pgsql::database (argc, argv));
  85.  
    #elif defined(DATABASE_ORACLE)
  86.  
    auto_ptr<database> db (new odb::oracle::database (argc, argv));
  87.  
    #elif defined(DATABASE_MSSQL)
  88.  
    auto_ptr<database> db (new odb::mssql::database (argc, argv));
  89.  
    #endif
  90.  
     
  91.  
    return db;
  92.  
    }
  93.  
     
  94.  
    #endif // DATABASE_HXX

 学新通

6、添加DATABASE_SQLITE宏

学新通

 7、main函数

  1.  
    #include "stdafx.h"
  2.  
    #include <memory> // std::auto_ptr
  3.  
    #include <iostream>
  4.  
     
  5.  
    #include <odb/database.hxx>
  6.  
    #include <odb/transaction.hxx>
  7.  
     
  8.  
    #include "database.hxx" // create_database
  9.  
     
  10.  
    #include "person.hxx"
  11.  
    #include "person-odb.hxx"
  12.  
     
  13.  
    using namespace std;
  14.  
    using namespace odb::core;
  15.  
     
  16.  
    int
  17.  
    main (int argc, char* argv[])
  18.  
    {
  19.  
    try
  20.  
    {
  21.  
    auto_ptr<database> db (create_database (argc, argv));
  22.  
     
  23.  
    unsigned long john_id, joe_id;
  24.  
     
  25.  
    // Create a few persistent person objects.
  26.  
    //
  27.  
    {
  28.  
    person john ("John", "Doe", 33);
  29.  
    person jane ("Jane", "Doe", 32);
  30.  
    person joe ("Joe", "Dirt", 30);
  31.  
     
  32.  
    transaction t (db->begin ());
  33.  
     
  34.  
    // Make objects persistent and save their ids for later use.
  35.  
    //
  36.  
    john_id = db->persist (john);
  37.  
    db->persist (jane);
  38.  
    joe_id = db->persist (joe);
  39.  
     
  40.  
    t.commit ();
  41.  
    }
  42.  
     
  43.  
    typedef odb::query<person> query;
  44.  
    typedef odb::result<person> result;
  45.  
     
  46.  
    // Say hello to those over 30.
  47.  
    //
  48.  
    {
  49.  
    transaction t (db->begin ());
  50.  
     
  51.  
    result r (db->query<person> (query::age > 30));
  52.  
     
  53.  
    for (result::iterator i (r.begin ()); i != r.end (); i)
  54.  
    {
  55.  
    cout << "Hello, " << i->first () << " " << i->last () << "!" << endl;
  56.  
    }
  57.  
     
  58.  
    t.commit ();
  59.  
    }
  60.  
     
  61.  
    // Joe Dirt just had a birthday, so update his age.
  62.  
    //
  63.  
    {
  64.  
    transaction t (db->begin ());
  65.  
     
  66.  
    auto_ptr<person> joe (db->load<person> (joe_id));
  67.  
    joe->age (joe->age () 1);
  68.  
    db->update (*joe);
  69.  
     
  70.  
    t.commit ();
  71.  
    }
  72.  
     
  73.  
    // John Doe is no longer in our database.
  74.  
    //
  75.  
    {
  76.  
    transaction t (db->begin ());
  77.  
    db->erase<person> (john_id);
  78.  
    t.commit ();
  79.  
    }
  80.  
     
  81.  
     
  82.  
    }
  83.  
    catch (const odb::exception& e)
  84.  
    {
  85.  
    cerr << e.what () << endl;
  86.  
    return 1;
  87.  
    }
  88.  
    }

 (1)存储

  1.  
    // Create a few persistent person objects.
  2.  
    //
  3.  
    {
  4.  
    person john ("John", "Doe", 33);
  5.  
    person jane ("Jane", "Doe", 32);
  6.  
    person joe ("Joe", "Dirt", 30);
  7.  
     
  8.  
    transaction t (db->begin ());
  9.  
     
  10.  
    // Make objects persistent and save their ids for later use.
  11.  
    //
  12.  
    john_id = db->persist (john);
  13.  
    db->persist (jane);
  14.  
    joe_id = db->persist (joe);
  15.  
     
  16.  
    t.commit ();
  17.  
    }

(2)查询

  1.  
    // Say hello to those over 30.
  2.  
    //
  3.  
    {
  4.  
    transaction t (db->begin ());
  5.  
     
  6.  
    result r (db->query<person> (query::age > 30));
  7.  
     
  8.  
    for (result::iterator i (r.begin ()); i != r.end (); i)
  9.  
    {
  10.  
    cout << "Hello, " << i->first () << " " << i->last () << "!" << endl;
  11.  
    }
  12.  
     
  13.  
    t.commit ();
  14.  
    }

(3)更新

  1.  
    // Joe Dirt just had a birthday, so update his age.
  2.  
    //
  3.  
    {
  4.  
    transaction t (db->begin ());
  5.  
     
  6.  
    auto_ptr<person> joe (db->load<person> (joe_id));
  7.  
    joe->age (joe->age () 1);
  8.  
    db->update (*joe);
  9.  
     
  10.  
    t.commit ();
  11.  
    }

(4)删除

  1.  
     
  2.  
    // John Doe is no longer in our database.
  3.  
    //
  4.  
    {
  5.  
    transaction t (db->begin ());
  6.  
    db->erase<person> (john_id);
  7.  
    t.commit ();
  8.  
    }

8、copy dll

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

学新通

 9、运行

学新通


 

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfhcheg
系列文章
更多 icon
同类精品
更多 icon
继续加载