日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

Qt笔记-QxOrm基本使用(对SQLLite进行增删改查)

發布時間:2025/3/15 数据库 54 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Qt笔记-QxOrm基本使用(对SQLLite进行增删改查) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這里本人使用的系統為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 <QxOrm.h> #include "export.h"#endif // PRECOMPILED_H

關鍵的是其qxDemo1.pro

include(./QxOrm/QxOrm.pri)INCLUDEPATH += D:/QtProject/QxOrm/include LIBS += -LD:\QtProject\QxOrm\libCONFIG(debug, debug|release) { TARGET = Demo LIBS += -l"QxOrmd" } else { TARGET = Demo LIBS += -l"QxOrm" })DEFINES += _BUILDING_QX_DEMO QT -= gui!contains(DEFINES, _QX_NO_PRECOMPILED_HEADER) { PRECOMPILED_HEADER = ./precompiled.h } # !contains(DEFINES, _QX_NO_PRECOMPILED_HEADER)CONFIG += c++11 console CONFIG -= 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}/bin else: 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_ptr<User> User_ptr; typedef qx::QxCollection<int, User_ptr> List_user;#endif // USER_H

user.cpp

#include <QCoreApplication> #include <QxOrm_Impl.h> #include <QDebug> #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>();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::QxCollection<int, User_ptr> List_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>(*user_2);return a.exec(); }

main.cpp

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

程序運行截圖如下:

這里是先添加了2條數據,然后再添加1條,再刪除一條,最后數據庫中文件是這樣的:

源碼打包下載地址:

https://github.com/fengfanchen/Qt/tree/master/QxOrmDemo1

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的Qt笔记-QxOrm基本使用(对SQLLite进行增删改查)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。