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 to make variable QSqlQuery available within the project
Forum Updated to NodeBB v4.3 + New Features

How to make variable QSqlQuery available within the project

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 2.8k Views 1 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.
  • V Offline
    V Offline
    VladimirPivovar
    wrote on last edited by
    #1

    I want to make available to the entire project type variable QSqlQuery. Can I do that? I need to save the state variable QSqlQuery within the entire project, so I could then create a query to a large insertion into the database using the method bind. Or do I have to start a separate class with collections (collections to fill and then transmit them to iterate through the bind method of the object QSqlQuery, announced in a separate method)

    if a variable is static QSqlQuery query as static - inside the method will still need to declare it again. Such a code query (db) the compiler does not pass. Once inside the method will write QSqlQuery query (db) - then there is no error.

    1 Reply Last reply
    0
    • V Offline
      V Offline
      VladimirPivovar
      wrote on last edited by
      #2

      I want to spread the code for different methods:

      / / db variable I have declared the entire project is available, as would

      @
      static QSqlDatabase db;

      QString queryString = "INSERT INTO mainpackage (answer,phoneNumberA,phoneNumberB,callDuration,dateOfCall,timeOfCall) VALUES ";
      queryString.append( QString( "(?,?,?,?,?,?)," ).repeated( N ) );
      queryString.chop( 1 );

      // despite the fact that the variable is a static query, it is not available in other methods:
      // QSqlQuery query (db)

      static QSqlQuery query = new QSqlQuery(db); // also not available in other methods

      if( !query.prepare( queryString ) )
      {
      qDebug() << "Unable to prepare query:" << query.lastError();
      return 1;
      }
      @

      The code I want to make a different method: (But should be available to the variable query, which was announced in the previous method)
      @
      for( int i = 0; i < N; ++i )
      {
      query.addBindValue(1);
      query.addBindValue( "123456789123456789" );
      query.addBindValue( "123456789123456789" );
      query.addBindValue( "12345" );
      query.addBindValue( "2012-05-26" );
      query.addBindValue( "19:00:00" );

      }

      if( !query.exec() )
      {
      qDebug() << "Unable to exec query:" << query.lastError();
      return 1;
      }
      @

      1 Reply Last reply
      0
      • F Offline
        F Offline
        Fahmy
        wrote on last edited by
        #3

        Maybe you can create special class to be include in your entire project.

        Stay hungry, stay foolish.

        1 Reply Last reply
        0
        • V Offline
          V Offline
          VladimirPivovar
          wrote on last edited by
          #4

          !http://imageshack.us/f/210/nomatchingfunction.jpg/()!

          1 Reply Last reply
          0
          • F Offline
            F Offline
            Fahmy
            wrote on last edited by
            #5

            I think it's better to put your code here rather than give screenshot link.

            Stay hungry, stay foolish.

            1 Reply Last reply
            0
            • W Offline
              W Offline
              Wilk
              wrote on last edited by
              #6

              Hello.
              I have two questions:

              What do you want to achive as a result?

              Why the hell are you using BOOL and TRUE (AFAIK from WINAPI) instead of just bool and true?

              Also, I think you'd better use "bindValue":http://qt-project.org/doc/qt-4.8/qsqlquery.html#bindValue because it makes your code cleaner.

              1 Reply Last reply
              0
              • V Offline
                V Offline
                VladimirPivovar
                wrote on last edited by
                #7

                Thank you! Worked!
                But why not work with the positional parameters (and only with named parameters in this case)?

                1 Reply Last reply
                0
                • W Offline
                  W Offline
                  Wilk
                  wrote on last edited by
                  #8

                  I don't know, but probably you should explicitly construct QVariant from qint.

                  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