SQLite_ORM 使用
QLite的编译
编译lib文件
如果您想要在生成lib库文件时指定平台为x64,可以使用Visual Studio提供的工具来完成。
- 打开Visual Studio的“Developer Command Prompt”(或者在安装目录中手动运行
vcvarsall.bat
脚本设置环境变量)。 - 在打开的命令提示符窗口中,导航到包含.def文件的目录。
- 运行以下命令来生成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;
评论区