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
QtWS25 Last Chance

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 4 Mar 2020, 11:07 last edited by aha_1980 3 Apr 2020, 12:40
    #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();
    }
    
    J 1 Reply Last reply 4 Mar 2020, 11:11
    0
    • B Babs
      4 Mar 2020, 11:07

      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();
      }
      
      J Offline
      J Offline
      JonB
      wrote on 4 Mar 2020, 11:11 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 4 Mar 2020, 11:17
      0
      • J JonB
        4 Mar 2020, 11:11

        @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 4 Mar 2020, 11:17 last edited by Babs 3 Apr 2020, 11:20
        #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

        J 1 Reply Last reply 4 Mar 2020, 11:22
        0
        • B Babs
          4 Mar 2020, 11:17

          @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

          J Offline
          J Offline
          JonB
          wrote on 4 Mar 2020, 11:22 last edited by JonB 3 Apr 2020, 11:25
          #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 4 Mar 2020, 11:32
          2
          • A Offline
            A Offline
            artwaw
            wrote on 4 Mar 2020, 11:24 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
            • J JonB
              4 Mar 2020, 11:22

              @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 4 Mar 2020, 11:32 last edited by Babs 3 Apr 2020, 11:35
              #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 4 Mar 2020, 13:16
              0
              • B Babs
                4 Mar 2020, 11:32

                @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 4 Mar 2020, 13:16 last edited by
                #7

                Thank you all i resolved my problem.

                Pablo J. RoginaP 1 Reply Last reply 4 Mar 2020, 19:02
                1
                • B Babs
                  4 Mar 2020, 13:16

                  Thank you all i resolved my problem.

                  Pablo J. RoginaP Offline
                  Pablo J. RoginaP Offline
                  Pablo J. Rogina
                  wrote on 4 Mar 2020, 19:02 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 5 Mar 2020, 10:11
                  1
                  • Pablo J. RoginaP Pablo J. Rogina
                    4 Mar 2020, 19:02

                    @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 5 Mar 2020, 10:11 last edited by Babs 3 May 2020, 10:17
                    #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 5 Mar 2020, 12:35
                    0
                    • B Babs
                      5 Mar 2020, 10:11

                      @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 5 Mar 2020, 12:35 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

                      1/10

                      4 Mar 2020, 11:07

                      • Login

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