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. Reusing QSqlQuery by using QSqlQuery::prepare()

Reusing QSqlQuery by using QSqlQuery::prepare()

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 1.6k 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
    PetrS82
    wrote on 3 Apr 2019, 15:11 last edited by
    #1

    Hello,
    is possible to reuse an instance of QSqlQuery by redefinition of query by calling QSqlQuery::prepare(newSQL_string) after query.exec()?

    If yes, is it neccessary to clear previous data by calling some function like QSqlQuery::clear() or QSqlQuery::finish() or ... before I will call prepare() function?

    Or is neccessary to use for each SQL query the new instance of QSqlQuery?

    Assume the code like this:

    query.prepare("INSERT INTO PERSONS (Name, City) VALUES ('John', 'Boston')");
    if (query.exec()) {
    query.prepare("SELECT * FROM CARS WHERE CAR_ID = 10");
    query.exec();
    }

    K K 2 Replies Last reply 3 Apr 2019, 17:10
    0
    • P PetrS82
      3 Apr 2019, 15:11

      Hello,
      is possible to reuse an instance of QSqlQuery by redefinition of query by calling QSqlQuery::prepare(newSQL_string) after query.exec()?

      If yes, is it neccessary to clear previous data by calling some function like QSqlQuery::clear() or QSqlQuery::finish() or ... before I will call prepare() function?

      Or is neccessary to use for each SQL query the new instance of QSqlQuery?

      Assume the code like this:

      query.prepare("INSERT INTO PERSONS (Name, City) VALUES ('John', 'Boston')");
      if (query.exec()) {
      query.prepare("SELECT * FROM CARS WHERE CAR_ID = 10");
      query.exec();
      }

      K Offline
      K Offline
      KroMignon
      wrote on 3 Apr 2019, 17:10 last edited by
      #2

      @PetrS82 Yes, no problem:

      QSqlQuery query(dbConnection);
      
      if(query.exec("INSERT INTO PERSONS (Name, City) VALUES ('John', 'Boston'))
      {
           if(query.exec("SELECT * FROM CARS WHERE CAR_ID = 10"))
          {
              while(query.next())
              {
                  // do stuff...
              }
          }
      }
      

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      1 Reply Last reply
      3
      • P PetrS82
        3 Apr 2019, 15:11

        Hello,
        is possible to reuse an instance of QSqlQuery by redefinition of query by calling QSqlQuery::prepare(newSQL_string) after query.exec()?

        If yes, is it neccessary to clear previous data by calling some function like QSqlQuery::clear() or QSqlQuery::finish() or ... before I will call prepare() function?

        Or is neccessary to use for each SQL query the new instance of QSqlQuery?

        Assume the code like this:

        query.prepare("INSERT INTO PERSONS (Name, City) VALUES ('John', 'Boston')");
        if (query.exec()) {
        query.prepare("SELECT * FROM CARS WHERE CAR_ID = 10");
        query.exec();
        }

        K Offline
        K Offline
        kshegunov
        Moderators
        wrote on 3 Apr 2019, 22:11 last edited by
        #3

        It's a good idea to call QSqlQuery::finish before you run your next prepare statement to tell the DB that it can release any locks or snapshots you may've acquired.

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        2
        • K Offline
          K Offline
          Kent-Dorfman
          wrote on 4 Apr 2019, 06:30 last edited by
          #4

          @PetrS82 said in Reusing QSqlQuery by using QSqlQuery::prepare():

          query.prepare("INSERT INTO PERSONS (Name, City) VALUES ('John', 'Boston')");
          if (query.exec()) {
          query.prepare("SELECT * FROM CARS WHERE CAR_ID = 10");
          query.exec();
          }

          Redefining the prepared statement query is a bad practice because it negates the objective of a prepared statement. Simply create another query object and keep your prepares linked to distinct query objects. If you are intent upon using a single query object then you might as well simply do immediate mode SQL in the exec("sql") fashion.

          K 1 Reply Last reply 4 Apr 2019, 09:08
          0
          • K Kent-Dorfman
            4 Apr 2019, 06:30

            @PetrS82 said in Reusing QSqlQuery by using QSqlQuery::prepare():

            query.prepare("INSERT INTO PERSONS (Name, City) VALUES ('John', 'Boston')");
            if (query.exec()) {
            query.prepare("SELECT * FROM CARS WHERE CAR_ID = 10");
            query.exec();
            }

            Redefining the prepared statement query is a bad practice because it negates the objective of a prepared statement. Simply create another query object and keep your prepares linked to distinct query objects. If you are intent upon using a single query object then you might as well simply do immediate mode SQL in the exec("sql") fashion.

            K Offline
            K Offline
            kshegunov
            Moderators
            wrote on 4 Apr 2019, 09:08 last edited by
            #5

            Unless you bind arguments, which arguably is much better than allowing for injection of invalid query data.

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            0

            1/5

            3 Apr 2019, 15:11

            • Login

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