DoubleLi

qq: 517712484 wx: ldbgliet

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  4737 随笔 :: 2 文章 :: 542 评论 :: 1615万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
#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

posted on   DoubleLi  阅读(582)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示