[solved] Application crash QSqlQuery(dbconnection) is called.

  • Hi. I have database connection named "sasm" defined in main.cpp. then in database.cpp i have assign connection to variable named dbCon. I successfuly created QSqlTabelModel using this variable. but when ever I used QSqlQuery(dbCon) application crashed. I couldn't find a solution. I am very grateful if someone can give solution soon. Thank you.

  • @sachi Can you show some code excerpts, please?

  • database.h
    #ifndef DATABASE_H
    #define DATABASE_H
    #include <QtSql>
    #include "message.h"
    #include <QSqlQuery>

    class Database

    bool AddLoginRecord(int usrID, int islogin);
    QSqlTableModel * CreateModel (QString table);

    QSqlDatabase dbCon;

    Message msg;


    #endif // DATABASE_H

    #include "database.h"

    dbCon = QSqlDatabase::database("sasm");



    // Create table model for given table
    QSqlTableModel * Database::CreateModel(QString table) {
    QSqlTableModel * model = new QSqlTableModel(0,dbCon);
    model->setTable('' + table + '');
    return model;

    bool Database::AddLoginRecord(int usrID, int islogin) {
    QSqlTableModel * model = new QSqlTableModel(0, dbCon);
    return true;

    main.cpp // create database connection
    bool CreateConnection() {
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", "sasm");
    if (!db.open()) {
    return false;
    } else {
    return true;

    when i Called createmodel function it works well. but any other function if i had call dbCon application will crash.

    "Starting /media/sac/other/private/projects/SASM/build-SASM-Desktop_Qt_5_4_0_GCC_64bit-Debug/SASM...
    The program has unexpectedly finished.
    /media/sac/other/private/projects/SASM/build-SASM-Desktop_Qt_5_4_0_GCC_64bit-Debug/SASM crashed"
    this is my error i got on qt creator

  • At first sight...

    In database.h you don't include QSqlDatabase or have a forward declaration for it. I'm going to assume you've added QT += sql in your .pro for it to compile . Usually if you press F5 QtCreator will show you the lines where it stopped at. (not a silver bullet, but at the very least it will give you an idea. (it can be messy).).

  • Lifetime Qt Champion


    Are you sure you are not accessing an uninitialized variable ? You use model in several function as local variable, does it shadow a class member ?

  • dbCon is initialized. if i use dbCon any function other than createmodel aplication crashed. I cant figure out whats wrong.

  • I found where i was wrong. I haven't initilized database object where this fuction was called. but right now dbCon is closed. i have open connection using dbCon.open(). but still dbCon.isOpen returns false.

  • Lifetime Qt Champion

    Is dbCon correctly configured before calling open ? What does lastError return ?

  • yes. database is configured correctly. i have created tablemodel and do the insert, edit etc. but in other functions it shows database is not open. lastError returns "driver not loaded". I am using "QMYSQL" driver. i chekced the output of QSqlDatabase::drivers(). It returns "QSQLITE", "QMYSQL", "QMYSQL3", "QPSQL", "QPSQL7".

    here is my two functions
    // Record login and logouts
    bool Database::AddLoginRecord(int usrID, int islogin) {
    QSqlQuery * query = new QSqlQuery(dbCon);
    // msg->Error(query->lastError());
    qDebug() << QSqlDatabase::drivers();
    query->prepare("INSERT INTO login_session VaLUES (NULL,?,?)");

    return query->exec();


    // Create table model for given table
    QSqlTableModel * Database::CreateModel(QString table) {
    QSqlTableModel * model = new QSqlTableModel(0,dbCon);
    model->setTable('' + table + '');
    return model;

    create model function works fine and i can accesse database. but when addLoginRecord called i got the error "database not open"

  • I just found problem is not with database class. it is with mainWindow class. if I create object using database class in mainWindow and called method from database.cpp. database connection show as invaild.

  • Lifetime Qt Champion

    Driver not loaded ? Do you have MySQL installed properly ?

  • yeah i have solved problem. its was at the main function. i have created database connection after the MainWindow is created. so mainWindow cannot use the database connection. By the way thanks for your help guys.

  • Lifetime Qt Champion

    Thanks for sharing your solution !

    Since you have it working now, please update the thread title prepending [solved] so other forum users may know a solution has been found :)

Log in to reply