QxOrm基本使用(对SQLLite进行增删改查) 全球时快讯

时间:2023-05-05 13:22:27来源 : QT教程

这里本人使用的系统为Win10,Qt为5.14,编译器为WinGW

这里先简单说明下首先是用WinGW编译下QxOrm源码:


(资料图片仅供参考)

编译好后会在lib下生成dll以及.a(我这里是使用WinGW)

新建一个项目,结构是这样的:

这里有几个关键的地方一个是QxOrm,相当于使用QxOrm的配置文件。

以及export.h和precompiled.h都是必备的,并且这个需要在profile文件中进行配置:

源码如下:

export.h

#ifndef PRECOMPILED_H#define PRECOMPILED_H#include #include \"export.h\"#endif // PRECOMPILED_H

关键的是其qxDemo1.pro

include(./QxOrm/QxOrm.pri)INCLUDEPATH += D:/QtProject/QxOrm/includeLIBS += -LD:\QtProject\QxOrm\libCONFIG(debug, debug|release) {TARGET = DemoLIBS += -l\"QxOrmd\"} else {TARGET = DemoLIBS += -l\"QxOrm\"})DEFINES += _BUILDING_QX_DEMOQT -= gui!contains(DEFINES, _QX_NO_PRECOMPILED_HEADER) {PRECOMPILED_HEADER = ./precompiled.h} # !contains(DEFINES, _QX_NO_PRECOMPILED_HEADER)CONFIG += c++11 consoleCONFIG -= app_bundle# The following define makes your compiler emit warnings if you use# any Qt feature that has been marked deprecated (the exact warnings# depend on your compiler). Please consult the documentation of the# deprecated API in order to know how to port your code away from it.DEFINES += QT_DEPRECATED_WARNINGS# You can also make your code fail to compile if it uses deprecated APIs.# In order to do so, uncomment the following line.# You can also select to disable deprecated APIs only up to a certain version of Qt.#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0SOURCES += \main.cpp \

user.cpp

# Default rules for deployment.qnx: target.path = /tmp/${TARGET}/binelse: unix:!android: target.path = /opt/${TARGET}/bin!isEmpty(target.path): INSTALLS += targetHEADERS += \export.h \precompiled.h \

user.h

这里有几个地方要注意的:

通过下面这个脚本配置好QxOrm

其次是设置好链接库目录和文件包含:

设置好对应的动态链接库:

定义好预编译文件:

其他代码如下:

user.h

#ifndef USER_H#define USER_Hclass QX_DEMO_DLL_EXPORT User{public:int id;QString name;int age;double capacity;User(): id(1){}virtual ~User(){}};QX_REGISTER_PRIMARY_KEY(User, int)QX_REGISTER_HPP_QX_DEMO(User, qx::trait::no_base_class_defined, 0)typedef std::shared_ptrUser_ptr;typedef qx::QxCollectionList_user;#endif // USER_H

user.cpp

#include #include #include #include \"precompiled.h\"#include \"user.h\"int main(int argc, char *argv[]){QCoreApplication a(argc, argv);QFile::remove(\"D:\QtProject\build-qxDemo1-Desktop_Qt_5_14_0_MinGW_32_bit-Debug\qxDemo1.sqlite\");qx::QxSqlDatabase::getSingleton()->setDriverName(\"QSQLITE\");qx::QxSqlDatabase::getSingleton()->setDatabaseName(\"D:\QtProject\build-qxDemo1-Desktop_Qt_5_14_0_MinGW_32_bit-Debug\qxDemo1.sqlite\");qx::QxSqlDatabase::getSingleton()->setHostName(\"localhost\");qx::QxSqlDatabase::getSingleton()->setUserName(\"root\");qx::QxSqlDatabase::getSingleton()->setPassword(\"\");qx::QxSqlDatabase::getSingleton()->setFormatSqlQueryBeforeLogging(true);qx::QxSqlDatabase::getSingleton()->setDisplayTimerDetails(true);qx::QxSqlDatabase::getSingleton()->setVerifyOffsetRelation(true);//更具上面类型创建表QSqlError daoError = qx::dao::create_table();User_ptr user_1, user_2;user_1.reset(new User);user_2.reset(new User);user_1->id = 1;user_1->name = \"猪小明\";user_1->age = 18;user_1->capacity = 99.9;user_2->id = 2;user_2->name = \"球球\";user_2->age = 18;user_2->capacity = 99999.9;QSqlDatabase db = qx::QxSqlDatabase::getDatabase();bool bCommit = db.transaction();//猪小明入库daoError = qx::dao::insert(user_1, &db);bCommit = (bCommit && ! daoError.isValid());qAssert(bCommit);db.commit();//球球入库daoError = qx::dao::save(user_2);bCommit = !daoError.isValid();qAssert(bCommit);//通过SQL进行检索,映射到 typedef qx::QxCollectionList_user;中List_user list_user;qx_query storedProc(\"select * from user\");daoError = qx::dao::execute_query(storedProc, list_user);List_user::iterator it = list_user.begin();qDebug() << \"------------------华丽的分割线------------------\";while(it != list_user.end()){qDebug() << \"id:\" << it.i->t().second->id;qDebug() << \"name:\" << it.i->t().second->name;qDebug() << \"age:\" << it.i->t().second->age;qDebug() << \"capacity:\" << it.i->t().second->capacity;it++;}qDebug() << \"------------------华丽的分割线------------------\";//修改下it = list_user.begin();while(it != list_user.end()){it.i->t().second->capacity = 59.9;it++;}qx::dao::update(list_user);//新增及删除User_ptr user_3;user_3.reset(new User);user_3->id = 100;user_3->name = \"闰土\";user_3->age = 19;user_3->capacity = 99999.9999;list_user.removeByKey(2);list_user.insert(100, user_3);qx::dao::save(user_3);qx::dao::delete_by_id(*user_2);return a.exec();}

main.cpp

#include \"precompiled.h\"#include \"user.h\"#include QX_REGISTER_CPP_QX_DEMO(User)namespace qx{template <>void register_class(QxClass&t){t.id(&User::id, \"id\");t.data(&User::age, \"age\");t.data(&User::name, \"name\");t.data(&User::capacity, \"capacity\");}}

程序运行截图如下:

【领 QT开发教程 学习资料, 点击下方链接莬费领取↓↓ ,先码住不迷路~】

点击这里:

关键词:

推荐内容

Back to Top