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]QSqlQuery::prepare segmentation fault.
Forum Updated to NodeBB v4.3 + New Features

[Solved]QSqlQuery::prepare segmentation fault.

Scheduled Pinned Locked Moved General and Desktop
12 Posts 4 Posters 7.7k 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.
  • L Offline
    L Offline
    luca
    wrote on last edited by
    #2

    I usually use "prepare" in conjunction with "bindValue".

    In your code why don't you use directly query.exec("....") ?

    1 Reply Last reply
    0
    • L Offline
      L Offline
      luca
      wrote on last edited by
      #3

      ...and why do you use mutex? Is you application multi-thread?

      1 Reply Last reply
      0
      • F Offline
        F Offline
        Fahmy
        wrote on last edited by
        #4

        Try this instead:

        QSqlQuery query;
        query.exec(QString("select id from table where identifer='%1'").arg(key));

        If you still want to use prepare(). Try like this:

        QSqlQuery query;
        query.prepare("select id from table where identifer = ?");
        query.addBindValue(key, QSql::Out);

        Stay hungry, stay foolish.

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

          In a multi-thread application I call the following code fore every thread:
          @
          db = new QSqlDatabase();
          m_nomeDb = QString::number(qrand());
          *db = QSqlDatabase::cloneDatabase(QSqlDatabase::database(), m_nomeDb);
          db->open();
          @

          and for every query in the same thread:
          @
          QSqlQuery query(*db);
          query.exec("select .... bla bla bla ...");
          ...
          query.next();
          ...
          ...
          @

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

            "Threads and the SQL module":http://qt-project.org/doc/qt-4.8/threads-modules.html#threads-and-the-sql-module

            1 Reply Last reply
            0
            • Q Offline
              Q Offline
              qxoz
              wrote on last edited by
              #7

              Thank you for replies!!!
              As shown by Lukas Geyer "A connection can only be used from within the thread that created it. ..."
              So i think solution of Luca prefered :).

              But now, i changed code like
              @query.exec(QString(“select id from table where identifer=’%1’”).arg(key));@
              and not get segmentation fault for now.

              Thank you again!

              1 Reply Last reply
              0
              • Q Offline
                Q Offline
                qxoz
                wrote on last edited by
                #8

                Luca what about memory leak
                @db = new QSqlDatabase();@
                when an object will deleted?

                Question 2:
                In this case(QSqlDatabase for every thread) Mutex are not needed?

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  luca
                  wrote on last edited by
                  #9

                  [quote author="qxoz" date="1353576031"]Luca what about memory leak
                  @db = new QSqlDatabase();@
                  when an object will deleted?
                  [/quote]

                  I usually put that code in the parent object contructor and this:
                  @
                  db->close();
                  delete db;
                  QSqlDatabase::removeDatabase(m_nomeDb);
                  @
                  in the destruct.

                  [quote author="qxoz" date="1353576031"]
                  Question 2:
                  In this case(QSqlDatabase for every thread) Mutex are not needed?
                  [/quote]

                  If you have two threads using two DB connections you don't need mutex to execute queries.

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    luca
                    wrote on last edited by
                    #10

                    If you want to do as I wrote, you also need to make a DB connection in your main :
                    @
                    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
                    db.setHostName(ip);
                    db.setDatabaseName(db_name);
                    db.setUserName(user);
                    db.setPassword(pwd);

                    db.open();
                    

                    @

                    this way you create a main DB connection and you can use it in the main thread. Next you can "clone" the connections in your threads.

                    1 Reply Last reply
                    0
                    • Q Offline
                      Q Offline
                      qxoz
                      wrote on last edited by
                      #11

                      I changed my code as you said, and it works ok :). Thanks!

                      1 Reply Last reply
                      0
                      • L Offline
                        L Offline
                        luca
                        wrote on last edited by
                        #12

                        You are welcome...

                        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