;相关下载
https://www.qxorm.com/qxorm_en/download.html
;QxModels.h
#include "precompiled.h"
/***************************************************************
* @projectName pluqt
* @brief 自定义ORM模型
* @author lzw
* @date 2022-01-04
***************************************************************/
struct User
{
long id;
QString name;
int age;
QString hobbies;
};
QX_REGISTER_HPP_QX_DLL1(User, qx::trait::no_base_class_defined, 1)
源文件:QxModels.cpp
#include "precompiled.h"
#include "qxmodels.h"
#include <QxOrm_Impl.h>
QX_REGISTER_CPP_QX_DLL1(User)
namespace
qx
{
template
<>
void
register_class(QxClass<User> & t)
{
// 设置表名
t.setName(
"User"
);
// 注册 User::id <=> 数据库中的主键
t.id(&User::id,
"id"
);
// 注册 User::name 属性,使用的 key 是 name,version 是 1。
t.data(&User::name,
"name"
, 1);
// 注册 User::age 属性,使用的 key 是 age。
t.data(&User::age,
"age"
);
// 注册 User::hobbies 属性,使用的 key 是 hobbies。
t.data(&User::hobbies,
"hobbies"
);
}
}
QString in_db = QCoreApplication::applicationDirPath();
in_db.append(
"/database/plulocal.db"
);
QFile::
remove
(in_db);
qx::QxSqlDatabase::getSingleton()->setDriverName(
"QSQLITE"
);
qx::QxSqlDatabase::getSingleton()->setDatabaseName(in_db);
qx::QxSqlDatabase::getSingleton()->setHostName(
"localhost"
);
qx::QxSqlDatabase::getSingleton()->setUserName(
"root"
);
qx::QxSqlDatabase::getSingleton()->setPassword(
""
);
qx::QxSqlDatabase::getSingleton()->setSqlPlaceHolderStyle(qx::QxSqlDatabase::ph_style_2_point_name);
qx::QxSqlDatabase::getSingleton()->setTraceSqlQuery(
true
);
qx::QxSqlDatabase::getSingleton()->setTraceSqlRecord(
false
);
// 建表
QSqlError daoError1 = qx::dao::create_table<User>();
// 产生100条模拟数据
for
(
int
in_idx=0; in_idx<100; ++in_idx)
{
auto in_user =
new
User();
in_user->name =
"lzw"
+QString::number(in_idx);
in_user->age = 20+in_idx;
in_user->hobbies =
"play"
;
auto daoError1 = qx::dao::insert(in_user);
}
// 查询单条记录
User in_pointUser; in_pointUser.id = 3;
qDebug()<<in_pointUser.name;
QSqlError daoError11 = qx::dao::fetch_by_id(in_pointUser);
qDebug()<<in_pointUser.name;
// 查询一定年龄段的集合记录
//typedef std::shared_ptr<User> UserPtr;
//typedef qx::QxCollection<long, UserPtr> UserList;
UserList in_userList;
qx_query in_query(
"select * from user where age>=20 and age<=25"
);
daoError11 = qx::dao::execute_query(in_query, in_userList);
qAssert(! daoError11.isValid()); qAssert(in_userList.count() > 0);
qx::dump(in_userList);
;序列化的两个函数
// 导出binary流
qx::serialization::qt::to_file(in_pointUser,
"user.txt"
);
// 导出json文本
qx::serialization::json::to_file(in_userList,
"list_of_user.json"
);
;过滤sql打印
https://www.zhihu.com/people/qi-hong-tao-30-81
https://www.ljjyy.com/archives/2023/05/100647
运行程序,会打印一系列sql执行的输出信息,QxOrm 不会隐藏 SQL 查询(默认情况下,所有的语句都会显示),所以在控制台中可以看到执行过程,如果想要关闭,可以使用官方建议的下面的方法,但是我测试了没有用,因此我用QT的自定义日志拦截器对 包含 QxOrm的日志进行了过滤。
qx::QxSqlDatabase::getSingleton()->setFormatSqlQueryBeforeLogging(false);
qx::QxSqlDatabase::getSingleton()->setVerifyOffsetRelation(false);
qx::QxSqlDatabase::getSingleton()->setTraceSqlQuery(false);
qx::QxSqlDatabase::getSingleton()->setTraceSqlBoundValuesOnError(false);
qx::QxSqlDatabase::getSingleton()->setTraceSqlRecord(false);
qx::QxSqlDatabase::getSingleton()->setTraceSqlBoundValues(false);
qx::QxSqlDatabase::getSingleton()->setTraceSqlBoundValuesOnError(false);
;自定义导出
https://www.cnblogs.com/chinasoft/p/16065959.html
https://www.qxorm.com/qxorm_en/manual_qxee.html
复制第一个的h和cpp到custom里面,增加 一下两个include
#include <models_export.gen.h>
#include <models_precompiled_header.gen.h>
;预览c++代码
pimpl不要勾选,会增加编译时间
;完整代码
int main(int argc, char * argv[])
{
// Qt application
QCoreApplication app(argc, argv);
QFile::remove("./qxBlog.sqlite");
// Parameters to connect to database
qx::QxSqlDatabase::getSingleton()->setDriverName("QSQLITE");
qx::QxSqlDatabase::getSingleton()->setDatabaseName("./qxBlog.sqlite");
qx::QxSqlDatabase::getSingleton()->setHostName("localhost");
qx::QxSqlDatabase::getSingleton()->setUserName("root");
qx::QxSqlDatabase::getSingleton()->setPassword("");
// Only for debug purpose : assert if invalid offset detected fetching a relation
qx::QxSqlDatabase::getSingleton()->setVerifyOffsetRelation(true);
// !!! TO CREATE TABLES, PLEASE USE THE C++ DDL SQL EXPORT PLUGIN OF QXENTITYEDITOR !!!
// Create all tables in database (you should not use 'qx::dao::create_table<T>()' function in a production software, use it just for prototypes or samples)
QSqlError daoError = qx::dao::create_table<author>();
daoError = qx::dao::create_table<comment>();
daoError = qx::dao::create_table<category>();
daoError = qx::dao::create_table<blog>();
// Create a list of 3 author
author_ptr author_1; author_1.reset(new author());
author_ptr author_2; author_2.reset(new author());
author_ptr author_3; author_3.reset(new author());
author_1->setlastname("author_1");
author_1->setsex(sex::male);
author_1->setbirthdate(QDateTime::currentDateTime());
author_2->setlastname("author_2");
author_2->setsex(sex::female);
author_2->setbirthdate(QDateTime::currentDateTime());
author_3->setlastname("author_3");
author_3->setsex(sex::female);
author_3->setbirthdate(QDateTime::currentDateTime());
list_of_author authorX;
authorX.insert(1, author_1);
authorX.insert(2, author_2);
authorX.insert(3, author_3);
// Insert list of 3 author into database
daoError = qx::dao::insert(authorX);
qAssert(qx::dao::count<author>() == 3);
// Clone author n°2 : 'author_id_2'
author_ptr author_clone = qx::clone(* author_2);
qAssert(author_clone->getauthor_id() == author_2->getauthor_id());
qAssert(author_clone->getsex() == sex::female);
// Create a query to fetch only female author : 'author_id_2' and 'author_id_3'
qx::QxSqlQuery query("WHERE t_author.sex = :sex");
query.bind(":sex", sex::female);
list_of_author list_of_female_author;
daoError = qx::dao::fetch_by_query(query, list_of_female_author);
qAssert(list_of_female_author.count() == 2);
// Dump list of female author (xml serialization)
qx::dump(list_of_female_author);
// Create 3 categories
category_ptr category_1 = category_ptr(new category("cat_1"));
category_ptr category_2 = category_ptr(new category("cat_2"));
category_ptr category_3 = category_ptr(new category("cat_3"));
category_1->setname("category_1"); category_1->setdescription("desc_1");
category_2->setname("category_2"); category_2->setdescription("desc_2");
category_3->setname("category_3"); category_3->setdescription("desc_3");
{ // Create a scope to destroy temporary connexion to database
// Open a transaction to database
QSqlDatabase db