ODBの使い方(How to use odb On windows)
4429 ワード
1.ODB libraryをダウンロードする:ODB Compler、Common Runtime Library、Database Runtime Library。
http://www.codesynthesis.com/products/odb/download.xhtml
(注意:ODB Complerはodb-x.x.i-686-windows、Database Runtime Libriesはあなたが使いたいデータベースに対応するライブラリです。)
2.ストレス解消、取り付け
ルートディレクトリでodbを実行してもいいし、環境変数に加えることもできます。
3.ヘッダファイルで定義されているクラスは、上は正常クラスで、下はodbに修正されたpersistentです。 クラス
http://www.codesynthesis.com/products/odb/download.xhtml
(注意:ODB Complerはodb-x.x.i-686-windows、Database Runtime Libriesはあなたが使いたいデータベースに対応するライブラリです。)
2.ストレス解消、取り付け
ルートディレクトリでodbを実行してもいいし、環境変数に加えることもできます。
3.ヘッダファイルで定義されているクラスは、上は正常クラスで、下はodbに修正されたpersistentです。 クラス
class person
{
...
private:
string email_;
string name_;
unsigned short age_;
};
#include <string>
#include <odb/core.hxx>
4.odbを利用してデータベースサポートコードを生成してこそ、上記で定義されたpersistent類を利用してクエリーを行うことができる。具体的なコマンドラインのパラメータ:http://www.codesynthesis.com/products/odb/doc/odb.xhtml#pragma db object class person { ... private: friend class odb::access; person () {} #pragma db id string email_; string name_; unsigned short age_; };
例えば:
odb--database mysql--generate-query D:\MCSF_Backup\MAIN\Feature\McsfProtocolDBWrapper\Mcsf ProtocolDBWrapper\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>
6.注意:// #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; } }
定義されたpersistent類の中で、いくつかの菗pragmaがよく使われています。
1)フィールドをプライマリキーとして指定します。
同前
db
id
2) フィールドをデータベースの対応するタイプに指定します。
同前
db
タイプ
(
「VRCHAR(64)binary not null」)
3)フィールドをデータベースの対応する列に指定します。
同前
db
column(
「Catalog Name」)
4)データベースの主キーの自己成長を指定します。
同前
db
id
autodb , persistent , -I 。
: