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的更多相关文章
随机推荐
- PHP:GD库 图片水印处理
文章来源:http://www.cnblogs.com/hello-tl/p/7592974.html <?php /** * 处理图片类 * 1.添加文字水印 * 2.添加图片水印 * 3.压 ...
- MSP430F5529时钟系统深究
1.为什么要进行时钟管理? 时钟系统是一个数字器件的命脉,对于普通的51单片机来说,它的时钟来源只有外部晶振,然后每12个振荡周期完成一个基本操作,所以也叫做12T单片机,但对于当前高级一点的单片机来 ...
- Django——配置服务器上线
使用UWSGI和NGINX配置项目上线 首先你得有一个拿得出手的项目 其次,购买了域名,也备案成功了 将settings.py中的DEBUG设置为False 配置Uwsgi 在项目(哪里都可以)中创建 ...
- 九度oj 题目1190:大整数排序
题目1190:大整数排序 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4142 解决:1867 题目描述: 对N个长度最长可达到1000的数进行排序. 输入: 输入第一行为一个整数N,( ...
- CentOS下LVS DR模式负载均衡配置详解
一安装LVS准备: 1.准备4台Centos 6.2 x86_64 注:本实验关闭 SELinux和IPtables防火墙. 管理IP地址 角色 备注 192.168.1.101 LVS主调度器(Ma ...
- JVM(三):深入分析Java字节码-上
JVM(三):深入分析Java字节码-上 字节码文章分为上下两篇,上篇也就是本文主要讲述class文件存在的意义,以及其带来的益处.并分析其内在构成之一 ---字节码,而下篇则从指令集方面着手,讲解指 ...
- 立面图 平面图 剖面图 CAD
http://www.qinxue.com/88.html http://www.xsteach.com/course/2855 前后左右各个侧面的外部投影图——立面图:对建筑物各个侧面进行投影所得到 ...
- 【.Net 学习系列】-- FileSystemWatcher 监控文件夹新生成文件,并在确认文件没有被其他程序占用后将其移动到指定文件夹
监控文件夹测试程序: using System; using System.Collections.Generic; using System.IO; using System.Linq; using ...
- spring 数据源JNDI 基于tomcat mysql配置
关键代码 <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean&q ...
- Oracle生成多表触发器sql
--将所有HY开头的表都生成一个更新触发器的脚本('/'是为了连续创建多个触发器而不报错)select 'CREATE OR REPLACE TRIGGER '||table_name||' BEFO ...