odb's view

//db.hxx:

#ifndef PG_HXX_INCLUDED

#define PG_HXX_INCLUDED

#include <memory>   // std::auto_ptr

#include <odb/database.hxx>

#include <odb/pgsql/database.hxx>

#include <odb/oracle/database.hxx>

namespace eachma{

namespace db{

    static const std::auto_ptr<odb::database> pgdb

         (new odb::pgsql::database ("tnt","pw","tnt","localhost",5432));

    static const std::auto_ptr<odb::database> oradb

         (new odb::oracle::database ("jw_xk","pwd","jwnew","211.69.240.2",1521));

}

};

#endif // DB_HXX_INCLUDED

hxx:

#ifndef JW_HXX_INCLUDED

#define JW_HXX_INCLUDED

#include <string>

#include <odb/core.hxx>

#include <odb/tr1/memory.hxx>

namespace eachma{

namespace jw{

#pragma db view query("SELECT XH, XM " \

"FROM JW.XJ_XJB WHERE XH LIKE (?)")

struct pupil

{

#pragma db type("VARCHAR(256)")

std::string xh;

#pragma db type("VARCHAR(256)")

std::string xm;

};

void print_pupils();

}}

#endif // JW_HXX_INCLUDED

cxx:

#include "jw.hxx"

#include <vector>

#include <odb/database.hxx>

#include <memory>

#include <odb/transaction.hxx>

#include "../utils/db.hxx"

#include "jw-odb.hxx"

#include <iostream>

namespace eachma

{

namespace jw

{

void print_pupils()

{

    try

    {

        odb::transaction t (eachma::db::oradb->begin ());

        odb::result<pupil> r(eachma::db::oradb->query<pupil>("'1410%'"));

        for (odb::result<pupil>::iterator i (r.begin ()); i != r.end (); ++i)

        {

            std::cout << i->xh << ", " << i->xm <<std::endl;

        }

        t.commit();

    }

    catch(const odb::exception& e)

    {

        std::cout << e.what()<<std::endl;

    }

}

}

}


你可能感兴趣的:(C++,view,odd,or)