QSqlDatabase:SIGSEV初始化错误

时间:2019-03-14 16:03:44

标签: c++ c++11 sqlite qt5

我从编译器收到SIGSEV错误,但不确定为什么。我正在尝试将示例数据保存在QTableView上。为此,我创建了一小组参数,并在类dataInfo中创建了一个initDataBase(const QString &nameDB)函数来初始化数据库。当我运行小的.ui时,编译器在QSqlDatabase::addDatabase("QSQLITE");处失败。另外,为了消除该选项之一,我在.pro文件中添加了QT += core gui sql语句作为标准过程,但还是没有运气。

我正在遵循的过程是:

1)设置如下参数:

imageparam.h

#ifndef IMAGEPARAM_H
#define IMAGEPARAM_H
#include <QString>
typedef struct Param
{
    int mId;
    QString mPath1;
    QString mPath2;
    QByteArray mImage1;
    QByteArray mImage2;
} Param;
class ImageParam
{
public:
    ImageParam(Param newdata);
    ImageParam(int id, const QString &path1, QString &path2,
               const QByteArray &image1, const QByteArray &image2);
    int id() const { return data.mId; }
    QString path1() const { return data.mPath1; }
    QString path2() const { return data.mPath2; }
    QByteArray image1() { return data.mImage1; }
    QByteArray image2() { return data.mImage1; }
private:
    Param data;
};

imageparam.cpp

#include "imageparam.h"
ImageParam::ImageParam(Param newdata)
{
    data = newdata;
}
ImageParam::ImageParam(int id, const QString &path1, QString &path2,
                       const QByteArray &image1, const QByteArray &image2)
{
    data.mId = id;
    data.mPath1 = path1;
    data.mPath2 = path2;
    data.mImage1 = image1;
    data.mImage2 = image2;
}

2)设置QSQLITE数据库

datainfo.h

#ifndef DATAINFO_H
#define DATAINFO_H
#include <QObject>
#include <QtSql/QSqlDatabase>
#include "imageparam.h"
class dataInfo : public QObject
{
    Q_OBJECT
public:
    explicit dataInfo(QObject *parent = nullptr);
    QString getError() const { return mError; }
    bool initDataBase(const QString &nameDB);
    bool confDataBase();
    bool addItem(ImageParam* imageItem);
    bool updateItem(int idItem, ImageParam* imageItem);
    QSqlDatabase getDatabase();
private:
    QString mError;
    QSqlDatabase mDBImages;

};

#endif // DATAINFO_H

datainfo.cpp

#include "datainfo.h"
#include <QtSql/QSqlQuery>
#include <QtSql/QSqlError>
#include <QDebug>
#include <QVariant>
#define CREATE_TABLE \
    " CREATE TABLE IF NOT EXISTS imageTable" \
    " (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL" \
    " path1 TEXT NOT NULL" \
    " path2 TEXT NOT NULL" \
    " imageA BLOB NOT NULL" \
    " imageB BLOB NOT NULL)"

dataInfo::dataInfo(QObject *parent) : QObject(parent)
{}

bool dataInfo::initDataBase(const QString &nameDB)
{
    mDBImages = QSqlDatabase::addDatabase("QSQLITE"); // <-- ERROR HERE
    mDBImages.setDatabaseName(nameDB);
    bool ok = mDBImages.open();
    if(!ok) {
        mError = mDBImages.lastError().text();
        qDebug() << mError;
    }
    return ok;
}

3)在mainwindow.cpp上,我的实例如下:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "datainfo.h"

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);

        // temporary folder
        temporaryFolder = "/home/to/Desktop/folder/tmp.db";
        QFile dbRem(temporaryFolder);
        dbRem.remove();
        mNewDatabaseImages->initDataBase(temporaryFolder); // <-- ERROR HERE
        mNewDatabaseImages->confDataBase();
        mNewTableImages = new QSqlTableModel(this, mNewDatabaseImages->getDatabase());
        mNewTableImages->setTable("imageTable");
        mNewTableImages->select();
        ui->bookMarkTableView->setModel(mNewTableImages);
        ui->bookMarkTableView->showColumn(true);
    }

对这段代码片段中缺少的内容有什么想法吗? 感谢您对此发表看法。

1 个答案:

答案 0 :(得分:0)

@ Emanuele,我认为您在SQL上缺少某些语法,请尝试在您 datainfo.cpp

上启用它
#include "datainfo.h"
#define CREATE_TABLE \
    " CREATE TABLE IF NOT EXISTS yourTable" \
    " (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL" \
    " path1 TEXT NOT NULL" \
    " path2 TEXT NOT NULL" \
    " imageA BLOB NOT NULL" \
    " imageB BLOB NOT NULL)"

具有以下代码

#include "datainfo.h"
#define CREATE_TABLE \
    " CREATE TABLE IF NOT EXISTS yourTable" \
    " (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL" \
    ", path1 TEXT NOT NULL" \
    ", path2 TEXT NOT NULL" \
    ", imageA BLOB NOT NULL" \
    ", imageB BLOB NOT NULL)"