Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Program crash when creating sqlite database



  • 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();
    }
    


  • @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?



  • @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



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



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



  • @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.



  • Thank you all i resolved my problem.



  • @Babs said in Program crash when creating sqlite database:

    Thank you all i resolved my problem.

    would you mind sharing the solution? Thanks.



  • @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?



  • @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.


Log in to reply