#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. MySQL-----增

    增 **创建用户** create user 'alex'@'192.168.1.1' identified by '123123'; create user 'alex'@'192.168.1.%' ...

  2. Spring核心技术(八)——Spring自动装载的注解

    本文针对自动装载的一些注解进行描述. 基于注解的容器配置 @Required注解 @Required注解需要应用到Bean的属性的setter方法上面,如下面的例子: public class Sim ...

  3. java项目连接access数据库

    1.导入Access_JDBC30.jar到项目中 jar包百度云链接:https://pan.baidu.com/s/10HFM3HomMArvfHjklA_1MA 密码:0qxp 项目名称-> ...

  4. JavaEE JDBC 怎么加载驱动

    JDBC怎么加载驱动 @author ixenos 分析 1.JDBC是一套连接数据库的接口(放在java.util.sql.Driver类中),不同的数据库依此接口各自实现Java连接到数据库的操作 ...

  5. MySQL最优配置文件模板·2016-11-28

    小伙伴们大爱的MySQL最优配置文件模板更新啦.对之前的MySQL最优配置文件·20160901做了一些修正,更为名至实归.可以通过此链接进行下载.当然,更欢迎同学们提出意见和建议,共同打造一个最优M ...

  6. noip模拟赛 写代码

    分析:这其实就是括号匹配题,一眼贪心题,不过一开始贪错了,以为([)]是合法的......其实括号之间不能嵌套. 一开始的想法是尽量往左边填左括号,因为每种括号的数量都确定了,那么左括号和右括号的数量 ...

  7. noip模拟赛 补兵

    分析:比较难想的一道dp题.要想补兵的数量最多,最后每个小兵的血量肯定是呈一个阶梯状的:i,i+1,i+2......i+k.那么记录一下每个血量i离它最近的小兵的血量是多少,记作cur[i].那么把 ...

  8. Inversion 归并求逆元

    bobo has a sequence a 1,a 2,…,a n. He is allowed to swap twoadjacent numbers for no more than k time ...

  9. 创建Django项目(二)——数据库配置

    2013-08-05 20:53:44|          1.数据库配置         举例是用MySQL数据库,首先在settings文件中做配置,如下: DATABASES = {     ' ...

  10. hadoop(2)hadoop配置

    hadoop入门(二) hadoop的配置 1.本地模式 2.伪分布式 3.分布式     一.配置linux环境: 1打开虚拟网络编辑器,选择 VMnet1 仅主机模式, 子网 IP 设为 192. ...