我是靠谱客的博主 轻松蜜蜂,最近开发中收集的这篇文章主要介绍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:QtProjectQxOrmlib

CONFIG(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.0

SOURCES += 
        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 += target

HEADERS += 
    export.h 
    precompiled.h 
    user.h

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

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

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

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

定义好预编译文件:

其他代码如下:

user.h

#ifndef USER_H
#define USER_H

class 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进行增删改查)的全部内容,希望文章能够帮你解决Qt笔记-QxOrm基本使用(对SQLLite进行增删改查)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(59)

评论列表共有 0 条评论

立即
投稿
返回
顶部