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.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.
  • Q Offline
    Q Offline
    qxoz
    wrote on 22 Nov 2012, 06:52 last edited by
    #1

    Hi everyone!
    I know that i'm doing something wrong, but in last 3 hours i can't figure out what exactly.
    While run method:
    @
    int uUserTask::GetId(QString key)
    {
    int retId = -1;
    QSqlQuery query;
    query.prepare(QString("select id from table where identifer='%1'").arg(key));
    QMutexLocker mxlock(&mutex);
    if(query.exec())
    {
    if(query.next())
    {
    retId = query.value(0).toInt();
    }
    }
    return retId;
    }@

    i got segmentation fault in qsql_mysql.cpp at body of prepare method(line 900):
    @
    r=mysql_stmt_prepare(d->stmt, encQuery.constData(), encQuery.length());
    @

    Segmentation fault happens not always, and i compared query strings in fault case with other case, they are identical. I think i'm using threads in a wrong way, but how?

    tools: QtSDK 1.2.1 Qt4.8.1
    Os: Kubuntu 12.04
    Db: MySQL 5.5.22

    Thank you!

    1 Reply Last reply
    0
    • L Offline
      L Offline
      luca
      wrote on 22 Nov 2012, 07:22 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 22 Nov 2012, 07:26 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 22 Nov 2012, 07:29 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 22 Nov 2012, 07:36 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 22 Nov 2012, 07:37 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 22 Nov 2012, 08:30 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 22 Nov 2012, 09:20 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 23 Nov 2012, 10:46 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 23 Nov 2012, 10:51 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 23 Nov 2012, 11:34 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 23 Nov 2012, 12:41 last edited by
                          #12

                          You are welcome...

                          1 Reply Last reply
                          0

                          1/12

                          22 Nov 2012, 06:52

                          • Login

                          • Login or register to search.
                          1 out of 12
                          • First post
                            1/12
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved