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] Application crash QSqlQuery(dbconnection) is called.
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
qsqlquerydatabase
13 Posts 4 Posters 4.2k Views 3 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 sachi

    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.

    Karen MorrisseyK Offline
    Karen MorrisseyK Offline
    Karen Morrissey
    wrote on last edited by
    #2

    @sachi Can you show some code excerpts, please?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sachi
      wrote on last edited by
      #3

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

      class Database
      {
      public:
      Database();

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

      private:
      QSqlDatabase dbCon;

      Message msg;
      

      };

      #endif // DATABASE_H

      database.cpp
      #include "database.h"

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

      }

      Database::~Database(){
      dbCon.close();
      }

      // Create table model for given table
      QSqlTableModel * Database::CreateModel(QString table) {
      QSqlTableModel * model = new QSqlTableModel(0,dbCon);
      model->setTable('' + table + '');
      model->select();
      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");
      db.setHostName("localhost");
      db.setPassword("");
      db.setUserName("
      ");
      db.setDatabaseName("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

      1 Reply Last reply
      0
      • David.GD Offline
        David.GD Offline
        David.G
        wrote on last edited by
        #4

        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).).

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #5

          Hi,

          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 ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • S Offline
            S Offline
            sachi
            wrote on last edited by
            #6

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

            1 Reply Last reply
            0
            • S Offline
              S Offline
              sachi
              wrote on last edited by
              #7

              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.

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #8

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

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  sachi
                  wrote on last edited by
                  #9

                  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,?,?)");
                  query->addBindValue(usrID);
                  query->addBindValue(islogin);

                  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"

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    sachi
                    wrote on last edited by
                    #10

                    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.

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11

                      Driver not loaded ? Do you have MySQL installed properly ?

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        sachi
                        wrote on last edited by
                        #12

                        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.

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #13

                          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 :)

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          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