odb+mysqlクライアントコンパイル環境インストール+使用例

2888 ワード

1、odbダウンロードアドレスhttps://www.codesynthesis.com/products/odb/download.xhtmlを選択し、Common Runtime Library->libodb-2.4.0、Database Runtime Libraries->libodb-mysql-2.4.0を選択します.
2、依存ライブラリのダウンロードhttps://www.codesynthesis.com/projects/libcutl/,  libcutl-1.10.0
3、libcutl-1.10.0をインストールし、ディレクトリに入って./configureを実行する.make ; make install;
4、libodb-2.4.0を再インストールし、ディレクトリに入って./configureを実行する.make ; make install;
5、最後にlibodb-mysql-2.4.0をインストールし、ディレクトリに入って./configureを実行する.make ; make install;  libmysql._が見つからない場合r字の間違い、sudo apt-get install libmysqld-devでいいです.
6、使用
6.1、準備、person.hxxファイルの作成
#include 
#include 
 
using namespace std;
 
#pragma db object no_id 
class person
{
    public:
        friend class odb::access;
    public:
        unsigned long id;        
        string name;
        string sex;
        unsigned short age;
 
};

6.2、文ファイルを生成し、次のコマンドを実行する
odb -d mysql --generate-query --generate-schema person.hxx

この文を実行すると、person-odb.cxx、person-odb.hxx、person-odb.ixx、person.sqlの4つのファイルが生成されます.person.sqlは、自動的に生成される確立テーブルのsql文です.他の3つは、オペレーティング・データベースでサポートされているコードです.--generate-queryオプションは、データ・クエリーの生成にサポートされるコードです.--generate-schemaオプションは、データ・テーブルの作成に関連するコードを生成します.
6.3、コンパイル用demo
#include 
 
#include 
#include 
 
 
#include 
 
#include "person-odb.hxx"
 
using namespace std;
using namespace odb::core;
 
int main (int argc, char* argv[])
{
    person aben, wenhao;
    aben.id = 1;
    aben.name = "aben";
    aben.sex = "man";
    aben.age = 23;
    wenhao.id = 2;
    wenhao.name = "wenhao";
    wenhao.sex = "man";
    wenhao.age = 22;
    
    typedef odb::query query;
    typedef odb::result result;
    try
    {
        shared_ptr<:mysql::database> db;
        db.reset(new odb::mysql::database ("root", "12345", "test"));
        transaction t (db->begin ());
        t.tracer (stderr_tracer);
        db->persist (aben);
        db->persist (wenhao);
        
        query q(query::id == 2);
 
        result r = db->query(q);
 
        for (auto i:r) 
        {
            cout << "{id:" << i.id << "  name:" << i.name << "   sex:" << i.sex << "   age:" << i.age << "}" << endl;
        }
        t.commit ();
    }
    catch (const odb::exception& e)
    {
        cerr << e.what () << endl;
    }

    return 0;
}

コンパイルg+-std=c++11-o driver test.cpp person-odb.cxx  -I ./  -L/usr/local/lib/-lodb-mysql -lodb -lcutl
 
 
参照可能https://blog.csdn.net/woaichanganba/article/details/79841356