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 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
    • artwawA Offline
      artwawA Offline
      artwaw
      wrote on last edited by artwaw
      #2

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

      For more information please re-read.

      Kind Regards,
      Artur

      H 1 Reply Last reply
      3
      • artwawA artwaw

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

        H Offline
        H Offline
        hobbyProgrammer
        wrote on last edited by
        #3

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

        artwawA 1 Reply Last reply
        0
        • H hobbyProgrammer

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

          artwawA Offline
          artwawA Offline
          artwaw
          wrote on 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
          0
          • artwawA artwaw

            @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 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
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on 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

              JonBJ 1 Reply Last reply
              3
              • VRoninV VRonin

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

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on 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
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on 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

                  artwawA 1 Reply Last reply
                  1
                  • VRoninV VRonin

                    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:");
                    
                    artwawA Offline
                    artwawA Offline
                    artwaw
                    wrote on 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

                    • Login

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