【二】ODB - C++ 单表插入(V1.02)

插入数据主要有如下几个步骤:
(1)创建数据库连接对象
(2)创建本地对象
(3)插入数据到表
(4)统一提交


// driver.cxx
//

#include    // std::auto_ptr
#include 

#include 
#include 

#include 

#include "person.hxx"
#include "person-odb.hxx"

using namespace std;
using namespace odb::core;

int
    main (int argc, char* argv[])
{
    try
    {
        //auto_ptr db (new odb::mysql::database (argc, argv));
        //连接数据库
        auto_ptr db (
            new odb::mysql::database (
            "root"     // database login name
            ,"123456" // database password
            ,"collect" // database name
            ,"localhost"
            ,13306
            ));
        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;
    }
}

这里写图片描述

原文地址:
http://www.codesynthesis.com/products/odb/doc/manual.xhtml#2.4

你可能感兴趣的:(数据库,ODB,C++)