How to make variable QSqlQuery available within the project



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



  • 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;
    }
    @



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





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



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



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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.