Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Program crashes by using a database close !!!
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 2.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    srikanth.koorma
    wrote on last edited by
    #1

    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
    @

    1 Reply Last reply
    0
    • C Offline
      C Offline
      clochydd
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved