Important: Please read the 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[])
        QGuiApplication app(argc, argv);
        AppWrapper wrapper;
        QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");//not dbConnection
            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 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()
 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);
        registerQtQmlTricksUiElements (&mEngine);
        registerQtQmlTricksSmartDataModel (&mEngine);
        mEngine.rootContext()->setContextProperty("plan", plan);
        qmlRegisterType<DateTimeValidator>("my.components", 1, 0, "DateTimeValidator");
            return false;
            return true;

    It crash when we get to the line

            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