Error -1 on execute query...



  • I trying to execute a query, as showed below, and I got error everytime!
    The code error is -1, and As showed at "Qsqlerror":http://qt-project.org/doc/qt-5.0/qsqlerror.html, this error "type cannot be determined.".

    @ QSqlDatabase database = SingletonSession::getDatabase();
    if (database.open())
    {
    QSqlQuery query("SELECT * FROM velocidadeCAN", database);
    query.exec();
    query.first();
    qDebug() << "Consulta: " << query.executedQuery();
    qDebug() << "Error: " << query.lastError();
    query.finish();
    }
    @

    Debug:
    Consulta: "SELECT * FROM velocidadeCAN"
    Error: QSqlError(-1, "", "")

    How can I make some debug, or check the database to validade all! I could open the database created by Qt on Firefox Plugin called "SQLite Manager" and do the same query and get the data from database without errors!



  • If you pass a non-empty query string to the QSqlQuery constructor the query is immediately executed. There is no need to explicitly call exec().

    Quite contrary to, I'm not sure if a call to exec() is only allowed if there has been a query prepared using prepare(). This might be the cause of your error.



  • Checking...



  • Nope, I remove the line "exec()" and the problem persists!



  • Hi,

    Before exec() you should call prepare()

    BR,



  • [quote author="dcbasso" date="1343328452"]Nope, I remove the line "exec()" and the problem persists![/quote]Well, you might step through then to actually find out what's going wrong.



  • There is no error on Code, It's something very weird!
    The last error is -1 but all data is there to be used....
    Thanks



  • @ QSqlDatabase database = SingletonSession::getDatabase();
    if (database.open()) //did you already open the database in your singleton?
    {
    QSqlQuery query("SELECT * FROM velocidadeCAN", database);
    ...
    @

    Instead try (you probably want to omit "yourConnectionName" if you used the default):
    @
    QSqlDatabase database = QSqlDatabase::database( "yourConnectionName" );
    if( database.isOpen() )
    {
    QSqlQuery query( database );
    if( !query.exec( "your SQL statement" ) )
    {
    qDebug() << "Consulta: " << query.executedQuery();
    qDebug() << "Error: " << query.lastError().text();
    }
    ...
    @

    ... just an idea on how to proceed.


Log in to reply
 

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