#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. 集训第五周动态规划 E题 LIS

    Description The world financial crisis is quite a subject. Some people are more relaxed while others ...

  2. Web的三大基石

    [HTML] 实现了Web页面. [URL] 1.url Uniform Resource Locator的缩写,称为统一资源定位符.通过URL可以访问到互联网上的一个资源.如:图片.视频.网页等.通 ...

  3. topcoder SRM 639 div2

    见这里 http://ygdtc.sinaapp.com/?p=257

  4. segv & mini coredump 调研

    1. mini coredump    a. segv      http://zh.scribd.com/doc/3726406/Crash-N-Burn-Writing-Linux-applica ...

  5. vim fulerformat的设置

    在vim中设置选项,有注释很容易明白: set laststatus=1 "2总显示最后一个窗口的状态行,1窗口多于一个时显示最后一个窗口的状态行,0不显示最后一个窗口的状态行 fulerf ...

  6. Ubuntu12.04之vi的问题

    版本:ubuntu12.04. 问题:vi不能正常使用方向键与退格键. 原因:ubuntu系统自带的 vi 不完整导致. 解决方法:安装完整的vi,sudo apt-get install vim-g ...

  7. CLR GC

    一.垃圾回收算法 每个应用程序都包含一组根(root),每个根都是一个存储位置,他要么为null,要么指向托管堆的一个对象,类型中定义的静态字段.局部变量.方法参数等都会被认为是根. 垃圾回收器(GC ...

  8. 2017-10-03-morning

    #include <algorithm> #include <cstring> #include <cstdio> inline void read(int &am ...

  9. 选择判断语句(switch)

    选择判断语句(switch) 一.switch语句格式 switch(表达式){ case 取值1: 执行语句: break: case 取值2: 执行语句: break: …...    defau ...

  10. Mac 系统引导过程概述 & BootCamp 的秘密

    http://bbs.feng.com/read-htm-tid-6890655.html