目 录CONTENT

文章目录

SQLite_ORM 使用

WenzhouXv
2023-09-25 / 0 评论 / 0 点赞 / 69 阅读 / 0 字

SQLite_ORM 使用

QLite的编译

SQLI下载地址

image-20230924055354624

编译lib文件

image-20230924055819357

如果您想要在生成lib库文件时指定平台为x64,可以使用Visual Studio提供的工具来完成。

  1. 打开Visual Studio的“Developer Command Prompt”(或者在安装目录中手动运行vcvarsall.bat脚本设置环境变量)。
  2. 在打开的命令提示符窗口中,导航到包含.def文件的目录。
  3. 运行以下命令来生成x64架构的lib库文件:
lib /DEF:sqlite3.def  /MACHINE:x64

SQLite_ORM常用方法

这个案例以uuid为主

#include <sqlite_orm.h>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>

创建数据库与表

using namespace sqlite_orm;

auto storage = make_storage("example.db",
    make_table("persons",
        make_column("id", &Person::id, primary_key().autoincrement()),
        make_column("uuid", &Person::uuid),
        make_column("name", &Person::name)
    ));

if (!storage.table_exists("persons"))
{
        storage.sync_schema();
}

创建UUID

boost::uuids::random_generator generator;
boost::uuids::uuid uuid = generator();
std::string uuidStr = boost::uuids::to_string(uuid); // Convert UUID to string

创建新增字段

Person person;
person.uuid = uuidStr;
person.name = "John Doe";

storage.insert(person);

删除

storage.remove_all<Person>(where(c(&Person::uuid) == uuidStr));

auto persons = storage.get_all<Person>(where(c(&Person::uuid) == "fef96fae-0a4c-4790-8073-05f2945becf7"));
	if (!persons.empty()) {
		auto& foundPerson = persons.front();
		foundPerson.name = "Jane Smith11";
		storage.update(foundPerson);
		std::cout << "Update successful." << std::endl;
	}
	else {
		std::cout << "Person not found." << std::endl;
	}

	auto persons = storage.get_all<Person>(where(c(&Person::uuid) == "fef96fae-0a4c-4790-8073-05f2945becf7"));
	if (!persons.empty()) {
		auto& foundPerson = persons.front();
		std::cout << "Get successful." << std::endl;
	}
	else {
		std::cout << "Person not found." << std::endl;
	}

统计

 auto count = storage.count<Person>(where(c(&Person::id) == uuidToCount));

    std::cout << "满足条件的记录数量为:" << count << std::endl;
0
  1. 支付宝打赏

    qrcode alipay
  2. 微信打赏

    qrcode weixin

评论区