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()
Forum Updated to NodeBB v4.3 + New Features

Reusing QSqlQuery by using QSqlQuery::prepare()

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 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.
  • P Offline
    P Offline
    PetrS82
    wrote on 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();
    }

    KroMignonK kshegunovK 2 Replies Last reply
    0
    • P PetrS82

      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();
      }

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on 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

        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();
        }

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on 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
        • Kent-DorfmanK Offline
          Kent-DorfmanK Offline
          Kent-Dorfman
          wrote on 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.

          kshegunovK 1 Reply Last reply
          0
          • Kent-DorfmanK Kent-Dorfman

            @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.

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on 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

            • Login

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