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] A little problem using qsql (qsqlite)
Forum Update on Monday, May 27th 2025

[solved] A little problem using qsql (qsqlite)

Scheduled Pinned Locked Moved General and Desktop
11 Posts 2 Posters 3.3k Views
  • 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.
  • P Offline
    P Offline
    panosk
    wrote on last edited by
    #2

    Try without the single quotes around the placeholder (':thestamp') or use a positional placeholder (?). See http://qt-project.org/doc/qt-4.8/qsqlquery.html#approaches-to-binding-values.

    1 Reply Last reply
    0
    • H Offline
      H Offline
      helgur
      wrote on last edited by
      #3

      I failed to mention I've already tried that

      @ QString SQL = "SELECT id FROM posts WHERE stamp = ?";
      QSqlQuery qriL(connection);
      qriL.prepare(SQL);
      qriL.bindValue(0, postID);
      if (!qriL.exec() && !error) {
      errorCode = 502;
      error = true;
      errorMessage = qri.lastError().text();
      }
      qriL.nextResult();
      int stamp = qriL.value(0).toInt();@

      doesn't work

      I also tried without single quotes in SQL statement, although it is a varchar (int stamp and database record stamp is not the same, my naming is a bit confusing, sorry) so it should be enclosed with quotes, yes?

      1 Reply Last reply
      0
      • P Offline
        P Offline
        panosk
        wrote on last edited by
        #4

        Is the id field included in your schema or are you trying to fetch the "hidden" rowid from sqlite? If you want sqlite's id, use "rowid" and not "id". Otherwise, you could post your schema.

        1 Reply Last reply
        0
        • H Offline
          H Offline
          helgur
          wrote on last edited by
          #5

          @
          -- Table: posts
          CREATE TABLE posts (
          id INTEGER PRIMARY KEY AUTOINCREMENT
          NOT NULL
          UNIQUE,
          stamp CHAR( 12 ) NOT NULL
          UNIQUE,
          date INT( 20 ) NOT NULL
          );
          @

          id is the field I am trying to fetch

          1 Reply Last reply
          0
          • P Offline
            P Offline
            panosk
            wrote on last edited by
            #6

            That's not table "posts" :)

            1 Reply Last reply
            0
            • H Offline
              H Offline
              helgur
              wrote on last edited by
              #7

              sorry, I went a bit fast there. fix'd

              1 Reply Last reply
              0
              • H Offline
                H Offline
                helgur
                wrote on last edited by
                #8

                Ok, I managed to solve it. I don't know why but reverting my code to:

                @ QString SQL = "SELECT id FROM posts WHERE stamp = ?";
                QSqlQuery qriL(connection);
                qriL.prepare(SQL);
                qriL.bindValue(0, postID);
                if (!qriL.exec() && !error) {
                errorCode = 502;
                error = true;
                errorMessage = qri.lastError().text();
                }
                /*
                qriL.nextResult();
                int stamp = qriL.value(0).toInt();
                */
                int stamp;
                while (qriL.next()){
                stamp = qriL.value(0).toInt();
                }

                    std::cout <<stamp;
                    std::cout <<"<br />";@
                

                Seems to have fixed the problem as it printed the correct id number in browser, before it just generated an obscure integer out of nowhere. I have a feeling I need to revisit this, but for now it works

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  panosk
                  wrote on last edited by
                  #9

                  Well, then check these:

                  • Make sure your insert statement is working ok and the postID is not indeed 0
                  • Check the connection you pass to your query to make sure the database is open
                  • Try to fetch the record without your error code block with sth like
                    @
                    int stamp;
                    if(qriL.first()) {
                    stamp = qriL.value(0).toInt();
                    }
                    @
                  1 Reply Last reply
                  0
                  • H Offline
                    H Offline
                    helgur
                    wrote on last edited by
                    #10

                    Well as you can see from the schema, id is set to auto increment so if it was indeed set to 0 something would be very off. Beside that as I have reported, it now returned with the valid id so why do you ask me to check if my insert statement works when it clearly works?

                    Database is opened at object initialization and closed in the destructor and is shared within the scope of the object. Is that a good approach? Any pitfalls?

                    I will try your approach later, although I suspect it won't set int stamp at all as it didn't do it before.

                    Thank you for your reply and effort in trying to solve my issue :)

                    1 Reply Last reply
                    0
                    • P Offline
                      P Offline
                      panosk
                      wrote on last edited by
                      #11

                      Hi,

                      My post was meant to be posted before yours, but anyway :)

                      I think the problem is improper use of "nextResult()":http://qt-project.org/doc/qt-4.8/qsqlquery.html#nextResult. I haven't ever used this function, but according to the docs you should use it for multiple result sets. This means that you still have to use next() to actually navigate through the rows of a result set and then fetch the values you need. Also, I'm not sure if this function is supported by the SQLITE driver.

                      Anyway, next() is what you need or first() for evaluating your simple test query.

                      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