在linux系统中使用QxOrm框架2:自己的工程使用QxOrm

2019-04-11  本文已影响0人  树蜂

在看本文之前,请先处理好QxOrm环境,详见这里

本文目的:
通过QxOrm,实现某类的增删改查。
本文目录:
一、环境
二、代码


一、环境

1、新建Qt项目

项目结构如下,主要添加红框中的5个文件


image.png

2、添加环境变量

为以后开发方便,在/etc/profile中添加QxOrm的根目录

export QXORM_DIR=/mnt/hgfs/vm_share/source/QxOrm_1.4.5/QxOrm

如果QtCreator中不生效,则重启虚拟机。

3、修改pro文件

添加内容如下

# --------- QxOrm start ---------
QMAKE_CXXFLAGS += -std=c++0x

include($$(QXORM_DIR)/QxOrm.pri)

TEMPLATE = app
INCLUDEPATH += $$(QXORM_DIR)/include/
LIBS += -L"$$(QXORM_DIR)/lib"


CONFIG(debug, debug|release) {
TARGET = qxBlogd
LIBS += -l"QxOrmd"
} else {
TARGET = qxBlog
LIBS += -l"QxOrm"
} # CONFIG(debug, debug|release)
# --------- QxOrm end ---------

二、代码

1、 precompiled.h

#ifndef _QX_BLOG_PRECOMPILED_HEADER_H_
#define _QX_BLOG_PRECOMPILED_HEADER_H_

#include <QxOrm.h>
// 原例子中通过export.h控制dll文件在不同时期的状态,
// 我们是在linux下使用,不确定是否需要。。。
//#include "export.h"

#endif // _QX_BLOG_PRECOMPILED_HEADER_H_

可以将一些一起用到的头文件放入该文件中,加速编译(官网解释)。

2、author.h

#ifndef _QX_BLOG_AUTHOR_H_
#define _QX_BLOG_AUTHOR_H_

#include "database/precompiled.h"

class QX_DLL_IMPORT_HELPER author
{
public:
// -- enum
   enum enum_sex { male, female, unknown };
// -- properties
   QString     m_id;
   QString     m_name;
   QDate       m_birthdate;
   enum_sex    m_sex;
// -- contructor, virtual destructor
   author() : m_id("0"), m_sex(unknown) { ; }
   virtual ~author() { ; }
// -- methods
   int age() const;
};

QX_REGISTER_PRIMARY_KEY(author, QString)
QX_REGISTER_HPP_IMPORT_DLL(author, qx::trait::no_base_class_defined, 0)

typedef qx::QxCollection<QString, author*> list_author;

#endif // _QX_BLOG_AUTHOR_H_

原例子中使用到了std::shared_ptr等c++11的函数,因为本人的环境不兼容,因此没用c++11的内容。

3、author.cpp

#include "author.h"

#include <QxOrm_Impl.h>
QX_REGISTER_CPP_IMPORT_DLL(author)

namespace qx {
    template <> void register_class(QxClass<author> & t)
    {
       t.id(& author::m_id, "author_id");

       t.data(& author::m_name, "name");
       t.data(& author::m_birthdate, "birthdate");
       t.data(& author::m_sex, "sex");

    //   t.fct_0<int>(std::mem_fn(& author::age), "age"); // using std::mem_fn() here is just a workaround for an issue with some versions of MSVC, it is not required with a full compliant C++11 compiler (http://stackoverflow.com/questions/23778883/vs2013-stdfunction-with-member-function)
       t.fct_0<int>(& author::age, "age");
    }
}

int author::age() const
{
   if (! m_birthdate.isValid()) { return -1; }
   return (QDate::currentDate().year() - m_birthdate.year());
}

4、 databasehelper.h

#ifndef DATABASEHELPER_H
#define DATABASEHELPER_H

#include <QObject>

class DatabaseHelper : public QObject
{
    Q_OBJECT
public:
    explicit DatabaseHelper(QObject *parent = 0);
    
signals:
    
public slots:
    
private:
    void init();
};

#endif // DATABASEHELPER_H

5、databasehelper.cpp

#include "databasehelper.h"
#include <QFile>
#include "precompiled.h"
#include <QDebug>
#include "../bean/author.h"
DatabaseHelper::DatabaseHelper(QObject *parent) :
    QObject(parent)
{
    init();
}


void DatabaseHelper::init()
{
    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("");
    qx::QxSqlDatabase::getSingleton()->setFormatSqlQueryBeforeLogging(true);

    // Only for debug purpose : assert if invalid offset detected fetching a relation
    qx::QxSqlDatabase::getSingleton()->setVerifyOffsetRelation(true);

    // Create all tables in database
    QSqlError daoError = qx::dao::create_table<author>();

    // Create a list of 3 author
//    author author_1; author_1.reset(new author());
//    author author_2; author_2.reset(new author());
//    author author_3; author_3.reset(new author());

    author author_1;
    author author_2;
    author author_3;

    author_1.m_id = "author_id_1"; author_1.m_name = "author_1";
    author_1.m_sex = author::male; author_1.m_birthdate = QDate::currentDate();
    author_2.m_id = "author_id_2"; author_2.m_name = "author_2";
    author_2.m_sex = author::female; author_2.m_birthdate = QDate::currentDate();
    author_3.m_id = "author_id_3"; author_3.m_name = "author_3";
    author_3.m_sex = author::female; author_3.m_birthdate = QDate::currentDate();

    list_author authorX;
    authorX.insert(author_1.m_id, &author_1);
    authorX.insert(author_2.m_id, &author_2);
    authorX.insert(author_3.m_id, &author_3);

    // 插入多条记录
    daoError = qx::dao::insert(authorX);
    // 插入一条记录
//    daoError = qx::dao::insert(author_1);

    // 删除一条记录
    daoError = qx::dao::delete_by_id(author_2);

    // 修改记录名称
    author_1.m_name = "jishufeng";
    daoError = qx::dao::save(author_1);

    // 通过id进行查询
    author author_4;
    author_4.m_id = "author_id_1";
    daoError = qx::dao::fetch_by_id(author_4);
    qDebug()<<"fetch name-->"<<author_4.m_name;

    // 通过sql语句进行查询
    qx_query _Sql("SELECT * FROM author");
    authorX.clear();
    qDebug()<<"count before-->"<<QString::number(authorX.count());
    daoError = qx::dao::execute_query(_Sql, authorX);
    qDebug()<<"count after-->"<<QString::number(authorX.count());
}

运行DatabaseHelper即可。


结束

上一篇下一篇

猜你喜欢

热点阅读