ODB 入门介绍(一)

  1. 编写person.hxx文件
    // file      : self/person.hxx
    // copyright : not copyrighted - public domain
    
    #ifndef PERSON_HXX
    #define PERSON_HXX
    
    #include <string>
    #include <cstddef> 
    
    #include <odb/core.hxx>			// 包含odb::access
    #pragma db object				// 告诉编译器这是一个 persistent class 
    class person
    {
    public:
    	person(const std::string& first,
    		const std::string& last,
    		unsigned short age)
    		: first_(first), last_(last), age_(age)
    	{
    	}
    
    	const std::string& first() const { return first_; }
    
    	const std::string& last() const { return last_; }
    
    	unsigned short age() const { return age_; }
    
    	void age(unsigned short age) { age_ = age; }
    
    private:
    	friend class odb::access;		// 让默认的构造函数和其他类成员能够访问access类
    	person() {}						// 这个构造函数在实例化一个persistent class时会用到
    
    	#pragma db id auto				// 用来指定一个id, auto指明这个id的值是由数据库自动分配的
    	unsigned long id_;
    	std::string first_;
    	std::string last_;
    	unsigned short age_;
    };
    
    #endif // PERSON_HXX

    几个主要的地方在代码中注释了

  2. 用odb.exe生成对应的文件  和sql文件  
    执行 odb -d mysql --generate-query --generate-schema person.hxx

    执行完毕后会生成几个文件

    将这些文件加到你的工程目录中

  3. main函数的编写

    // file      : selfTest/driver.cxx
    // copyright : not copyrighted - public domain
    
    #include <memory>   // std::auto_ptr
    #include <iostream>
    
    #include <odb/database.hxx>
    #include <odb/transaction.hxx>
    
    #include "database.hxx"
    //#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 (create_database (argc, argv));
    		auto_ptr<odb::database> db(
    			new odb::mysql::database("root", "123456", "odbtest", "localhost", 3306)
    		);
    
    		unsigned long john_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);
    			db->persist(jane);
    			joe_id = db->persist(joe);
    
    			t.commit();
    		}
    
    
    
    	}
    	catch (const odb::exception& e)
    	{
    		cerr << e.what() << endl;
    		return 1;
    	}
    }
    

    3.1  查询
    persist(value) 这个函数就相当于把value存在一个缓存里, 当执行完commit() 之后会执行sql语句 将这些数据保存到数据库中odb::query<> 查询相关的类

    		typedef odb::query<person> query;
    		typedef odb::result<person> result;
    
    		transaction t(db->begin());
    		result r(db->query<person>(query::age >= 30));

    db->query<person>(query::age>=30) 意思是从数据中找 age>=30的所有行, 返回值是一个结果集 用 result r接住
    使用的时候就像使用迭代器一样使用这个结果集

    result::iterator it = r.begin();
    for (; it != r.end(); ++it)
    {
    	cout << "hello, " << it->first() << it->last() << " !" << endl;
    }

    3.2 更新 update
    看person.hxx中 有这个意思就是我们可以对这个字段进行更新赋值

    {
        transaction t(db->begin());
        person* pPerson = db->load<person>(joe_id);
    	pPerson->age(pPerson->age() + 1);
    	db->update(*pPerson);
    	t.commit();
    }

      db->load<person>(joe_id) 就是查询joe_id的数据 返回的是一个person*的指针
      然后调用 db->update(*pPerson)就可以这个玩家的数据, 执行t.commit()之后就可以更新到数据库了
    3.3  聚合查询的结果
    在文件person.hxx中添加结构体

    #pragma db view object(person)
    struct person_stat
    {
    	#pragma db column("count(" + person::id_ + ")")
    	std::size_t count;
    
    	#pragma db column("min(" + person::age_ + ")")
    	unsigned short min_age;
    
    	#pragma db column("max(" + person::age_ + ")")
    	unsigned short max_age;
    };

    重新生成odb -d mysql --generate-query --generate-schema person.hxx 文件 
    然后在main文件中添加代码

    transaction t(db->begin());
    person_stat ps(db->query_value<person_stat>());
    cout << endl
        << "count  : " << ps.count << endl
    	<< "min age: " << ps.min_age << endl
    	<< "max age: " << ps.max_age << endl;
    t.commit();

    这种方法是针对总是查询返回一个元素的结果, 如果这样写就会方便很多, 一次把所有要查询的单一元素聚合起来
    3.4 删除

    transaction t(db->begin());
    
    db->erase<person>(john_id);
    
    t.commit();

    通过key john_id 删除对应的行

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值