0

Everytime I declare QSqlDatabse connection in the header file of my DatabaseManager class, my application crashes with a message says: the program has unexpectedly finished ?!

If I put the declaration inside the source file, my application works fine.

  1. Why is this happening and
  2. how to fix it ?

Edit:

here is the header file :

#ifndef DATABASEMANAGER_H
#define DATABASEMANAGER_H

#include <QObject>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>

class DatabaseManager : public QObject
{
    Q_OBJECT
public:
    explicit DatabaseManager(QObject *parent = 0);

public:
    QString open_db();
    QString create_db_tables();
    QSqlDatabase get_db();

    QSqlDatabase database;
};

#endif // DATABASEMANAGER_H

here is the source file :

#include "databasemanager.h"
#include <QDir>
#include <QCoreApplication>
#include <QDebug>


QString DatabaseManager::open_db()
{

    QSqlDatabase db;
     db = database;

    QString path = "/Users/abubakr/Documents/workspace/Muasaa/";

    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(path+"Database v.1");
    if (db.open()){
        return "Database is created and open, Application is ready ...";
    } else {
        return db.lastError().text();
    }
}

QSqlDatabase DatabaseManager::get_db(){
}

QString DatabaseManager::create_db_tables(){

    QSqlQuery query;

    //****************************************
    //create personal_info table
    //****************************************
    query.prepare("CREATE TABLE IF NOT EXISTS personal_Info"
                  "(ref_no NTEGER UNIQUE PRIMARY KEY,"
                  "name VARCHAR(30), father VARCHAR(30), grandfather VARCHAR(30), mother VARCHAR(30), tel VARCHAR(30),"
                  "email VARCHAR(30), post_code VARCHAR(30), address VARCHAR(30))");
    if (!query.exec()) return query.lastError().text();

    //****************************************
    //create familly_info table
    //****************************************
    query.prepare("CREATE TABLE IF NOT EXISTS familly_Info"
                  "(ref_no NTEGER UNIQUE PRIMARY KEY,"
                  "spouse VARCHAR(30), father_in_law VARCHAR(30), mother_in_law VARCHAR(30), childern  VARCHAR(30))");
    if (!query.exec()) return query.lastError().text();


    //****************************************
    //create payment_info table
    //****************************************
    query.prepare("CREATE TABLE IF NOT EXISTS payment_Info"
                  "(ref_no NTEGER UNIQUE PRIMARY KEY,"
                  "payment VARCHAR(30), payment_method VARCHAR(30))");
    if (!query.exec()) return query.lastError().text();


    return "Tables created successfully ";

}
McLan
  • 2,552
  • 9
  • 51
  • 85
  • little code would be better here! – ramtheconqueror Nov 30 '15 at 17:17
  • Debugging does not show me anything ..@drescherjm – McLan Nov 30 '15 at 17:17
  • Is this on windows? If so are you using Qt that was built for your compiler version. You can not use Qt that was built for any other version. – drescherjm Nov 30 '15 at 17:18
  • 1
    ***Debugging does not show me anything*** You should be able to set your debugger to break on access violation (and other exceptions). Then step to the code causing the error. – drescherjm Nov 30 '15 at 17:19
  • @ramtheconqueror check the question-edit please – McLan Nov 30 '15 at 17:20
  • @drescherjm ... nice tip, thank you .. – McLan Nov 30 '15 at 17:22
  • 2
    There is no any relation between db object and query object. Your db instance exists inside of open_db() method only and query instance - inside create_db_tables(). You need to initialise db class member inside the class constructor or inside open_db() and than initialise query(db) inside create_db_tables(). – Max Fomichev Nov 30 '15 at 17:58
  • Thanks @MaxFomichev .. it works .. – McLan Dec 01 '15 at 23:51

1 Answers1

0

I think there are a couple things wrong here.

In the open_db() method keep in mind how memory management in C++ works. Nothing that gets defined there will survive. So for the following lines:

QSqlDatabase db;
db = database;

what you're actually doing is defining db, then setting it to the value of database, which is nothing. You then initialize db to the values you want, but that doesn't matter because when the method returns that will all get destroyed automatically. The database variable never gets set to anything. In the open_db() just initialize the variable you've defined in your class instead like this:

database = QSqlDatabase::addDatabase("QSQLITE");
database .setDatabaseName(path+"Database v.1");
if (database .open()){

then in create_db_tables() you have to initialize the QSqlQuery object with the database you just created like this:

QSqlQuery query(database);

Once you do that everything else should start working properly.

mabeechen
  • 121
  • 5