[solved]qt crashes on running QSqlQuery::exec()



  • qt is crashinbg when QSqlQuery::exec() it is charshing for any query bit if i remove it the program will work fine after debugging i got that the last task at which it chrashed was QSqlResult the program crashes on startup
    @
    #include "historymanager.h"
    #include <QDebug>
    historymanager::historymanager(QObject *parent) :
    QObject(parent)
    {
    database = QSqlDatabase::addDatabase("QSQLITE");
    database.setHostName("cute browser");
    database.setUserName("cute browser");
    database.setPassword("password");
    bool ok = database.open();
    qDebug() << ok;
    createtablehistory();
    query = new QSqlQuery(database);
    }
    bool historymanager::createtablehistory()
    {
    uery->exec("create table history(id integer primary key,firstname varchar(20),lastname varchar(30)age integer)");
    }
    bool historymanager::addhistory(QString title, QString url)
    {
    query->exec("insert into history(title ,url)VALUES(" + title + "," + url + ")");
    }
    @
    the code



  • Hello, ~developer!

    Add @Q_CHECK_PTR(query)@ before every @query->exec(..);@ instance. If you have false in your App Output - it should be SIGSEGV on broken pointer.



  • sorry man it is not working



  • the pointer is fine as it does not outputs anything



  • You call <code>createtablehistory()</code> at #12, which uses <code>query</code>, but <code>query</code> is initialized in line #13. Swap both.

    Q_CHECK_PTR() does not trap because a pointer, as almost every other variable, is not initialized with null (or initialized at all, unless explicitly done so); it will be purely coincidental if an unitialized pointer is null and therefore trapped by Q_CHECK_PTR().



  • it works lukas geyer i did not see that you are awesome thanks man



  • You're welcome.

    Feel free to prepend the post title with [Solved] (just edit your initial post), as an indication that your problem has been solved and that there is a solution inside.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.