#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的更多相关文章

随机推荐

  1. python的学习之路(三)

    一.set集合#!/usr/bin/env python# *_*coding:utf-8 *_*# Author: harson old_dict = { "#1": {'hos ...

  2. MySQL数据库初识

    认识数据库 1 什么是数据(Data) 描述事物的符号记录称为数据,描述事物的符号既可以是数字,也可以是文字.图片,图像.声音.语言等,数据由多种表现形式,它们都可以经过数字化后存入计算机 在计算机中 ...

  3. STM32——NVIV:嵌套中断向量控制器

    STM32有43个channel的settable的中断源:AIRC(Application Interrupt and Reset Register)寄存器中有用于指定优先级的4 bits.这4个b ...

  4. POJ 2718 Smallest Difference(贪心 or next_permutation暴力枚举)

    Smallest Difference Description Given a number of distinct decimal digits, you can form one integer ...

  5. jsp+servlet+mysql增删改查

    用的IntelliJ IDEA开发的,jdk1.8 1 首先是项目结构,如下图所示 2看各层的代码 首先是web.xml <?xml version="1.0" encodi ...

  6. Flask组件:flask-sqlalchemy & flask-script & flask-migrate

    flask-sqlalchemy组件 项目目录结构: flask目录 # 项目名 |--- flaskdir |--- static # 静态文件 |--- templates # 模板 |--- m ...

  7. [K/3Cloud] 代码中设置某个字段必录

    Control ctl = this.GetControl(fieldKey); FieldEditor editCtl = ctl as FieldEditor; if (editCtl != nu ...

  8. 【BZOJ3110】K大数查询(权值线段树套线段树+标记永久化,整体二分)

    题意:有N个位置,M个操作.操作有两种,每次操作 如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c 如果是2 a b c形式,表示询问从第a个位置到第b个位置,第C大的数是 ...

  9. idea使用之maven本地索引更新

    idea使用起来的确挺顺手的,但遇到一些问题时,直接百度很难有答案,只能自己慢慢摸索了. 今天在倒腾idea仓库索引的时候,就出事情啦. 先说idea更新本地仓库索引吧,打开settings--> ...

  10. Linux下汇编语言学习笔记31 ---

    这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...