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. Program crash when creating sqlite database

Program crash when creating sqlite database

Scheduled Pinned Locked Moved Solved General and Desktop
qtsql
10 Posts 4 Posters 1.5k Views
  • 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.
  • B Offline
    B Offline
    Babs
    wrote on last edited by aha_1980
    #1

    Hello,
    I'm working on a QML application that need a datebase base. The problem is when i create my database in main.cpp the program crash.
    Here is my code:

    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        QGuiApplication app(argc, argv);
        AppWrapper wrapper;
        QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");//not dbConnection
        db.setDatabaseName("C:/Users/a.diaw/Documents/sql/databases/db_modrian.sqllite");
        if(!db.open()){
            qDebug()<<"problem opening database";
        }
        /*QSqlQuery query;
        query.exec("create table person "
                  "(id integer primary key, "
                  "firstname varchar(20), "
                  "lastname varchar(30), "
                  "age integer)");*/
    
        if ( !wrapper.initialize())
            return -1;
    
        return app.exec();
    }
    
    JonBJ 1 Reply Last reply
    0
    • B Babs

      Hello,
      I'm working on a QML application that need a datebase base. The problem is when i create my database in main.cpp the program crash.
      Here is my code:

      int main(int argc, char *argv[])
      {
          QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
          QGuiApplication app(argc, argv);
          AppWrapper wrapper;
          QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");//not dbConnection
          db.setDatabaseName("C:/Users/a.diaw/Documents/sql/databases/db_modrian.sqllite");
          if(!db.open()){
              qDebug()<<"problem opening database";
          }
          /*QSqlQuery query;
          query.exec("create table person "
                    "(id integer primary key, "
                    "firstname varchar(20), "
                    "lastname varchar(30), "
                    "age integer)");*/
      
          if ( !wrapper.initialize())
              return -1;
      
          return app.exec();
      }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Babs said in Program crash when creating sqllite database:

      the program crash

      "Crash" in what way? System error message? (Or do you mean the db.open() just fails?) If it crashes, run from debugger and look at traceback?

      B 1 Reply Last reply
      0
      • JonBJ JonB

        @Babs said in Program crash when creating sqllite database:

        the program crash

        "Crash" in what way? System error message? (Or do you mean the db.open() just fails?) If it crashes, run from debugger and look at traceback?

        B Offline
        B Offline
        Babs
        wrote on last edited by Babs
        #3

        @JonB when i comment the part of the code about the database creation my program lauch the application window without problem. When i don't it crash before the

        return app.exec()
        

        db.open() doesn't fail

        JonBJ 1 Reply Last reply
        0
        • B Babs

          @JonB when i comment the part of the code about the database creation my program lauch the application window without problem. When i don't it crash before the

          return app.exec()
          

          db.open() doesn't fail

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @Babs
          Then I don't know, as your scope seems OK. No idea what else your code might do (e.g. wrapper.initialize()). So as I said why don't you run from debugger and look at stack trace, that's what it's for instead of guessing? (Even putting in qDebug()s to show which statement it is/is not reaching is better than now.)

          B 1 Reply Last reply
          2
          • artwawA Offline
            artwawA Offline
            artwaw
            wrote on last edited by
            #5

            Hi,
            as @JonB said - run in debug. Try step-by-step execution to see where exactly does it fail and hopefully you'll see what's wrong.

            For more information please re-read.

            Kind Regards,
            Artur

            1 Reply Last reply
            1
            • JonBJ JonB

              @Babs
              Then I don't know, as your scope seems OK. No idea what else your code might do (e.g. wrapper.initialize()). So as I said why don't you run from debugger and look at stack trace, that's what it's for instead of guessing? (Even putting in qDebug()s to show which statement it is/is not reaching is better than now.)

              B Offline
              B Offline
              Babs
              wrote on last edited by Babs
              #6

              @JonB the programm indeed crash in wrapper.initialize()
              Here is the code:

              bool AppWrapper::initialize()
              {
              
                  Planning *plan= new Planning(this);
                  mModel.setDataSource(plan);
                  registerQtQmlTricksUiElements (&mEngine);
                  registerQtQmlTricksSmartDataModel (&mEngine);
                  mEngine.rootContext()->setContextProperty("myModel",&mModel);
                  qmlRegisterType<PalierModel>("TableModel",0,1,"TableModel");
                  mEngine.rootContext()->setContextProperty("plan", plan);
                  qmlRegisterType<DateTimeValidator>("my.components", 1, 0, "DateTimeValidator");
                  mEngine.load(QUrl(QStringLiteral("qrc:/main.qml")));
                  ```
              if(mEngine.rootObjects().isEmpty()){
                      return false;
                  }
              
                  else{
                      return true;
                  }
              
              }
              

              It crash when we get to the line

              if(mEngine.rootObjects().isEmpty()){
                      return false;
                  }
              

              Here is the error message
              12:35:05: The process was ended forcefully.
              12:35:05: C:\Users\a.diaw\Documents\build-PlanningModrian-Desktop_Qt_5_14_1_MinGW_64_bit-Debug\Debug\modrian.57fb0ae3\undefinedmodrian.exe crashed.

              B 1 Reply Last reply
              0
              • B Babs

                @JonB the programm indeed crash in wrapper.initialize()
                Here is the code:

                bool AppWrapper::initialize()
                {
                
                    Planning *plan= new Planning(this);
                    mModel.setDataSource(plan);
                    registerQtQmlTricksUiElements (&mEngine);
                    registerQtQmlTricksSmartDataModel (&mEngine);
                    mEngine.rootContext()->setContextProperty("myModel",&mModel);
                    qmlRegisterType<PalierModel>("TableModel",0,1,"TableModel");
                    mEngine.rootContext()->setContextProperty("plan", plan);
                    qmlRegisterType<DateTimeValidator>("my.components", 1, 0, "DateTimeValidator");
                    mEngine.load(QUrl(QStringLiteral("qrc:/main.qml")));
                    ```
                if(mEngine.rootObjects().isEmpty()){
                        return false;
                    }
                
                    else{
                        return true;
                    }
                
                }
                

                It crash when we get to the line

                if(mEngine.rootObjects().isEmpty()){
                        return false;
                    }
                

                Here is the error message
                12:35:05: The process was ended forcefully.
                12:35:05: C:\Users\a.diaw\Documents\build-PlanningModrian-Desktop_Qt_5_14_1_MinGW_64_bit-Debug\Debug\modrian.57fb0ae3\undefinedmodrian.exe crashed.

                B Offline
                B Offline
                Babs
                wrote on last edited by
                #7

                Thank you all i resolved my problem.

                Pablo J. RoginaP 1 Reply Last reply
                1
                • B Babs

                  Thank you all i resolved my problem.

                  Pablo J. RoginaP Offline
                  Pablo J. RoginaP Offline
                  Pablo J. Rogina
                  wrote on last edited by
                  #8

                  @Babs said in Program crash when creating sqlite database:

                  Thank you all i resolved my problem.

                  would you mind sharing the solution? Thanks.

                  Upvote the answer(s) that helped you solve the issue
                  Use "Topic Tools" button to mark your post as Solved
                  Add screenshots via postimage.org
                  Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                  B 1 Reply Last reply
                  1
                  • Pablo J. RoginaP Pablo J. Rogina

                    @Babs said in Program crash when creating sqlite database:

                    Thank you all i resolved my problem.

                    would you mind sharing the solution? Thanks.

                    B Offline
                    B Offline
                    Babs
                    wrote on last edited by Babs
                    #9

                    @Pablo-J-Rogina i resolved the problem by moving the database creation in wrapper.initialize(). I assume that it's all about events in Qt. I don't understand yet this mechanism may be any one can explain it?

                    Pablo J. RoginaP 1 Reply Last reply
                    0
                    • B Babs

                      @Pablo-J-Rogina i resolved the problem by moving the database creation in wrapper.initialize(). I assume that it's all about events in Qt. I don't understand yet this mechanism may be any one can explain it?

                      Pablo J. RoginaP Offline
                      Pablo J. RoginaP Offline
                      Pablo J. Rogina
                      wrote on last edited by
                      #10

                      @Babs said in Program crash when creating sqlite database:

                      I don't understand yet this mechanism may be any one can explain it?

                      Please create a new post with this subject, so not to hijack this thread. Thanks.

                      Upvote the answer(s) that helped you solve the issue
                      Use "Topic Tools" button to mark your post as Solved
                      Add screenshots via postimage.org
                      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                      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