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. [solved]qt crashes on running QSqlQuery::exec()
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 3.6k Views 1 Watching
  • 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.
  • D Offline
    D Offline
    developer
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tucnak
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        developer
        wrote on last edited by
        #3

        sorry man it is not working

        1 Reply Last reply
        0
        • D Offline
          D Offline
          developer
          wrote on last edited by
          #4

          the pointer is fine as it does not outputs anything

          1 Reply Last reply
          0
          • L Offline
            L Offline
            lgeyer
            wrote on last edited by
            #5

            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().

            1 Reply Last reply
            0
            • D Offline
              D Offline
              developer
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • L Offline
                L Offline
                lgeyer
                wrote on last edited by
                #7

                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.

                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