[Solved] Program crashes by using a database close !!!



  • Hello everyone, am having an issue with using database close function. Can anyone of you help me out please.??

    The problem exactly is with the close function call in the serverapp destructor which crashes the program abruptly with an application output like

    @The program has unexpectedly finished.@

    You can check the code below

    serverapp.cpp:

    @
    #include "serverapp.h"
    #include "ui_serverapp.h"

    #include <QMessageBox>

    #include <QtWidgets>
    #include <QtSql>
    #include <QSqlQuery>

    QSqlDatabase db = QSqlDatabase::database();
    QSqlQuery *query;

    ServerApp::ServerApp(QWidget *parent):QMainWindow(parent),Ui::ServerApp()
    {
    setupUi(this);
    if(createConnection())
    {
    qDebug() << "Create Connection executed";
    statusLabel->setText(tr("Database Created and Opened"));
    }
    }

    ServerApp::~ServerApp()
    {
    query->finish();
    delete query;
    // query = 0;
    db.close();
    }

    bool ServerApp::createConnection()
    {
    qDebug() << "Adding database\n";
    db = QSqlDatabase::addDatabase("QSQLITE");
    qDebug() << "Setting database name\n";
    db.setDatabaseName("/home/chocky/Desktop/server.db");
    if (!db.open())
    {
    qDebug() << "Database not opened\n";
    QMessageBox::critical(0, qApp->tr("Cannot open database"),qApp->tr("Unable to establish a database connection.\nThis example needs SQLite support. Please read the Qt SQL driver documentation for information how to build it.\nClick Cancel to exit."),QMessageBox::Cancel);
    return false;
    }
    else
    {
    qDebug() << "Database opened\n";
    query = new QSqlQuery(db);
    if(query->exec("create table person (id int primary key, firstname varchar(20), lastname varchar(20))"))
    qDebug() << "Query executed";
    else
    qDebug() << "Query not executed";
    // query->finish();
    return true;
    }
    }
    @

    main.cpp:

    @
    #include "serverapp.h"
    #include <QApplication>

    #include <stdlib.h>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    ServerApp w;
    w.show();

    return a.exec&#40;&#41;;
    

    }
    @

    serverapp.h:

    @
    #ifndef SERVERAPP_H
    #define SERVERAPP_H
    #include "ui_serverapp.h"
    #include <QMainWindow>

    #include <QtSql>
    #include <QSqlDatabase>
    #include <QSqlError>
    #include <QSqlQuery>
    #include <QMessageBox>

    namespace Ui {
    class ServerApp;
    }

    class ServerApp : public QMainWindow, public Ui::ServerApp
    {
    Q_OBJECT

    public:
    ServerApp(QWidget *parent = 0);
    ~ServerApp();
    bool createConnection();

    private slots:
    void updateRB(QString);

    private:
    FortuneServer server;
    QSqlTableModel model;
    };

    #endif // SERVERAPP_H
    @

    ServerApp.pro:

    @
    #-------------------------------------------------

    Project created by QtCreator 2014-09-29T00:41:43

    #-------------------------------------------------

    QT += core gui network sql widgets

    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

    TARGET = ServerApp
    TEMPLATE = app

    SOURCES += main.cpp
    serverapp.cpp

    HEADERS += serverapp.h

    FORMS += serverapp.ui
    @



  • Hi,
    you should close and remove your database by using:
    @
    db = QSqlDatabase();
    QSqlDatabase::removeDatabase(db.connectionName());
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.