odb_sqlite_demo
| #include <iostream> | |
| #include <odb/database.hxx> | |
| #include <odb/transaction.hxx> | |
| #include <odb/schema-catalog.hxx> | |
| #include <odb/sqlite/database.hxx> | |
| #include "person.hpp" | |
| #include "person-odb.hxx" | |
| using namespace std; | |
| using namespace odb::core; | |
| void create_person_table(shared_ptr<odb::sqlite::database> db) | |
| { | |
| 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 (); | |
| } | |
| } | |
| void query_person(shared_ptr<odb::sqlite::database> db) | |
| { | |
| typedef odb::query<person> query; | |
| transaction t (db->begin()); | |
| auto r (db->query<person>(query::age > 30)); | |
| for (auto i:r){ | |
| cout << "Hello, " << i.first() << "!" << endl; | |
| } | |
| t.commit (); | |
| } | |
| shared_ptr<odb::sqlite::database> open_database(string name, bool create=false) | |
| { | |
| int flags = SQLITE_OPEN_READWRITE; | |
| if (create) flags |= SQLITE_OPEN_CREATE; | |
| shared_ptr<odb::sqlite::database> db(new odb::sqlite::database(name, flags) ); | |
| transaction t (db->begin()); | |
| if (create){ | |
| odb::schema_catalog::create_schema(*db); | |
| } | |
| t.commit (); | |
| return db; | |
| } | |
| shared_ptr<odb::sqlite::database> open_create_database(string name) | |
| { | |
| std::shared_ptr<odb::sqlite::database> db; | |
| try{ | |
| db = open_database(name); | |
| }catch (const odb::exception& e){ | |
| db = open_database(name,true); | |
| } | |
| return db; | |
| } | |
| int main (int argc, char* argv[]) | |
| { | |
| try{ | |
| auto db = open_create_database("test.db"); | |
| create_person_table(db); | |
| query_person(db); | |
| } | |
| catch (const odb::exception& e){ | |
| cerr << e.what () << endl; | |
| return 1; | |
| } | |
| return 0; | |
| } |
from:https://github.com/joseprous/odb-sqlite-test/blob/master/driver.cpp
odb_sqlite_demo的更多相关文章
随机推荐
- Android BottomSheet:List列表或Grid网格展示(3)
Android BottomSheet:List列表或Grid网格展示(3) BottomSheet可以显示多种样式的底部弹出面板风格,比如常见的List列表样式或者Grid网格样式,以一个例子 ...
- ORACLE ASH/AWR
(一) ASH 用户在ORACLE数据库中执行操作时,必然要创建相应的连接和会话,其中,所有当前的会话信息都保存在动态性能视图V$SESSION中,通过该视图,DBA可以查看用户实际执行的操作,或者当 ...
- 转盘抽奖 canvas & 抽奖 H5 源码
转盘抽奖 canvas https://github.com/givebest/wechat-turntalbe-canvas https://blog.givebest.cn/GB-canvas-t ...
- [K/3Cloud] 理解BOS关于Enabled属性的表决器原理
通常的编程中,我们习惯: btnOK.Enabled = true; 这个样子就会将按钮变成有效,反之亦然.但在ERP的表单中,其某个按钮或字段其有效性及其复杂,例如一个表格中某个数量单元格其有效性是 ...
- 贝尔数--Codeforces908E. New Year and Entity Enumeration
给n<=50个长度m<=1000的二进制数,记他们为集合T,求满足下面条件的集合S数:令$M=2^m-1$,1.$a \epsilon S \Rightarrow a \ \ xor \ ...
- HDU 5643 King's Game 【约瑟夫环】
题意: 变形的约瑟夫环,最初为每个人编号1到n,第i次删去报号为i的人,然后从它的下一个人开始重新从1开始报号,问最终剩下第几号人? 分析: 首先看一下裸的约瑟夫环问题: 共n个人,从1开始报数,报到 ...
- UVA 10564_ Paths through the Hourglass
题意: 由0-9的数字组成一个形如沙漏的图形,要求从第一行开始沿左下或者右下到达最后一行,问有多少种不同的路径,使最后路径上的整数之和为给定的某个数. 分析: 简单计数dp,从最后一行开始,设dp[i ...
- 携程Apollo(阿波罗)配置中心使用Google代码风格文件(在Eclipse使用Google代码风格)(配合阿里巴巴代码规约快速设置)
Apollo默认使用了Google的代码风格,文件放在这里: https://github.com/ctripcorp/apollo/tree/master/apollo-buildtools/sty ...
- JSP的安全性
以下内容引用自http://wiki.jikexueyuan.com/project/jsp/security.html: JavaServer Pages和Servlets有几种可用的机制可以使We ...
- JSP发送电子邮件
以下内容引用自http://wiki.jikexueyuan.com/project/jsp/sending-email.html: 发送一个简单的电子邮件 给出一个简单的例子,从机器上发送一个简单的 ...