数据库创建

// 创建表结构

inline auto initstorage(const std::string &path) {

return make_storage(

path,make_index("id", &entity::id),

make_table("entity",

make_column("primary_id", &entity::primary_id, primary_key(),

autoincrement(), default_value("-1")),

make_column("id", &entity::id),

make_column("type", &entity::type),

make_column("context", &entity::context),

make_column("start_at", &entity::start_at),

make_column("update_at", &entity::update_at)));

}

创建表时要指明存储的文件名,如“db.sqlite" 和表名 如 “users”,“user_types”,若要存储在内存里,文件名为":memory:"或者为空

可为列添加的属性,如自增autoincrement(),主键 primary_key(),初始值default_value(“0”)

查找:

auto templ_entity = this->dbptr_->select(

columns(&entity::primary_id, &entity::id, &entity::type,

&entity::context, &entity::start_at,

&entity::update_at),

where(is_equal(&entity::id, id)));

entity_gather = this->dbptr_->get_all<entity>();

新增:

this->dbptr_->transaction([&] {

auto id_entity = this->dbptr_->insert(

entity{tpl.primary_id, tpl.id, tpl.type, tpl.context,

tpl.start_at, tpl.update_at});

insert_entity.id = id_entity;

return true;});

this->dbptr_->transaction([&] {

this->dbptr_->update(insert_entity);

return true;

});

删除:

this->dbptr_->transaction([&] {

this->dbptr_->remove_all<entity>(

where(is_equal(&entity::id, id)));

return true;

});

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值