如何使用ODB(How to use odb On windows)

來源:互聯網
上載者:User

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;intmain (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添加路徑。
比如:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.