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. How can I qDebug a QSqlQuery?
Forum Updated to NodeBB v4.3 + New Features

How can I qDebug a QSqlQuery?

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 1.6k Views 2 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.
  • H Offline
    H Offline
    hobbyProgrammer
    wrote on 28 Nov 2019, 11:38 last edited by
    #1

    Currently I have no connection to my database, but I'd like to check the query I created using query.prepare() and query.bindValue().

    So lets say my code is:

        QSqlQuery *query = new QSqlQuery();
    
        query->prepare("INSERT INTO tblpeople(`name`, `lastname`, `age`") VALUES (" :name", ":lastname", ":age");
        query->bindValue(":name", ui->lineEdit);
        query->bindValue(":lastname", ui->lineEdit_2);
        query->bindValue(":age", ui->LineEdit_3);
    

    I already tried :

        qDebug() << query;
    

    but that resulted in a hexadecimal number. Does anyone know how I can debug this into (for example):

    INSERT INTO tblpeople(name, lastname, age) VALUES ("jessica", "smith", "21");

    1 Reply Last reply
    0
    • A Offline
      A Offline
      artwaw
      wrote on 28 Nov 2019, 11:49 last edited by artwaw
      #2

      query.executedQuery(); or query.lastQuery();

      For more information please re-read.

      Kind Regards,
      Artur

      H 1 Reply Last reply 28 Nov 2019, 12:01
      3
      • A artwaw
        28 Nov 2019, 11:49

        query.executedQuery(); or query.lastQuery();

        H Offline
        H Offline
        hobbyProgrammer
        wrote on 28 Nov 2019, 12:01 last edited by
        #3

        @artwaw That doesn't seem to work without a database connection. They're both empty.

        A 1 Reply Last reply 28 Nov 2019, 12:06
        0
        • H hobbyProgrammer
          28 Nov 2019, 12:01

          @artwaw That doesn't seem to work without a database connection. They're both empty.

          A Offline
          A Offline
          artwaw
          wrote on 28 Nov 2019, 12:06 last edited by
          #4

          @hobbyProgrammer Right. I missed the part when you say you have no connection to DB. I am sorry.
          I never tried but would guess that QSqlQuery::boundValues() can be used?

          For more information please re-read.

          Kind Regards,
          Artur

          H 1 Reply Last reply 28 Nov 2019, 12:09
          0
          • A artwaw
            28 Nov 2019, 12:06

            @hobbyProgrammer Right. I missed the part when you say you have no connection to DB. I am sorry.
            I never tried but would guess that QSqlQuery::boundValues() can be used?

            H Offline
            H Offline
            hobbyProgrammer
            wrote on 28 Nov 2019, 12:09 last edited by
            #5

            @artwaw nope unfortunately. I think I really need that database connection.. I've been busy with that for almost a week now, so I just wanted to create some code without having to connect to a database, but there's no other way for me to check if the code works properly.

            1 Reply Last reply
            0
            • V Offline
              V Offline
              VRonin
              wrote on 28 Nov 2019, 12:12 last edited by
              #6

              See https://stackoverflow.com/questions/5777409/how-to-get-last-prepared-and-executed-query-using-qsqlquery

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              J 1 Reply Last reply 28 Nov 2019, 12:43
              3
              • V VRonin
                28 Nov 2019, 12:12

                See https://stackoverflow.com/questions/5777409/how-to-get-last-prepared-and-executed-query-using-qsqlquery

                J Offline
                J Offline
                JonB
                wrote on 28 Nov 2019, 12:43 last edited by JonB
                #7

                @VRonin
                Your proposed answer(s) is the nicest, but unfortunately don't think any there will won't work as the OP has no connection and is not executing any queries :) I think all there rely on lastQuery() & exec(), or does one not? OP should look through them....

                1 Reply Last reply
                0
                • V Offline
                  V Offline
                  VRonin
                  wrote on 28 Nov 2019, 13:34 last edited by VRonin
                  #8

                  AFAIK lastQuery() doesn't need the query to have been executed so it's just a matter of replacing the placeholders with the bound values.

                  If OP still feels it would be better to have a db connection in place you can just use a dummy in-memory SQLite database:

                  QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
                  db.setDatabaseName(":memory:");
                  

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  A 1 Reply Last reply 28 Nov 2019, 13:39
                  1
                  • V VRonin
                    28 Nov 2019, 13:34

                    AFAIK lastQuery() doesn't need the query to have been executed so it's just a matter of replacing the placeholders with the bound values.

                    If OP still feels it would be better to have a db connection in place you can just use a dummy in-memory SQLite database:

                    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
                    db.setDatabaseName(":memory:");
                    
                    A Offline
                    A Offline
                    artwaw
                    wrote on 28 Nov 2019, 13:39 last edited by
                    #9

                    @VRonin I suggested lastQuery() but OP said it comes out empty:

                    @hobbyProgrammer said in How can I qDebug a QSqlQuery?:

                    @artwaw That doesn't seem to work without a database connection. They're both empty.

                    For more information please re-read.

                    Kind Regards,
                    Artur

                    1 Reply Last reply
                    1

                    1/9

                    28 Nov 2019, 11:38

                    • Login

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