odb_sqlite_demo

简介: #include       #include   #include   #include       #include       #include "person.
#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

目录
相关文章
|
6月前
|
关系型数据库 Java MySQL
【Sqlite】sqlite安装与与使用图文详解
【Sqlite】sqlite安装与与使用图文详解
120 0
|
5月前
|
数据库管理 Windows
【CodeSmith】The System.Data.SQLite library is not installed on this computer,不能使用SQLite解决办法
【CodeSmith】The System.Data.SQLite library is not installed on this computer,不能使用SQLite解决办法
21 0
|
数据库
安装SQLite3
安装SQLite3
189 0
|
SQL 数据库连接 数据库
『SQLite』C#/.NET EF+SQLite.CodeFirst——真正实现CodeFirst
📣读完这篇文章里你能收获到 - 本文详细介绍了SQLite实现CodeFirst的方式 - 通过代码配合图文的形式进行讲解
459 0
『SQLite』C#/.NET EF+SQLite.CodeFirst——真正实现CodeFirst
|
SQL 数据库 数据库管理
sqlite3——sqlite3应用相关函数
sqlite3——sqlite3应用相关函数
230 0
|
SQL Oracle 关系型数据库
玩转SQLite1:SQLite简介与安装
SQLite,是一个C语言库,诞生于2000年,它实现了一个小型、 快速、 自包含、 高可靠性的SQL数据库引擎,与其他数据库管理系统(如SQL Server或Oracle)的一大区别,是它非常的轻量级(小于500Kb大小)。
玩转SQLite1:SQLite简介与安装
|
存储 SQL 缓存
我为什么用 SQLite 和 FMDB 而不用 Core Data
凭良心讲,我不能告诉你不去使用Core Data。它不错,而且也在变好,并且它被很多其他Cocoa开发者所理解,当有新人加入你的组或者需要别人接手你的项目的时候,这点很重要。 更重要的是,不值得花时间和精力去写自己的系统去代替它。真的,使用Core Data吧。
149 0
|
数据库管理
SQLite 的版本问题
原文:SQLite 的版本问题 在SQLite官方网站上的下载包真可以看花眼。不同的.net版本,还有不同的平台,开发和发布时需要加以注意。
1192 0