;相關下載
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);
;自定義導出
http://www.rzrgm.cn/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