if that is the case then everything seems ok,try show output of nome,and declare it out side while statement.
if that is the case then everything seems ok,try show output of nome,and declare it out side while statement.
I've seen the problem! I put subestitui db.setDatabaseName by line 3 ("/ Users / danielsousa / Qt / BaseDados / bd_agt");
However I want to use only the name of the database that is to take on multiple operating systems. How do I do that?
regards,
Daniel Sousa
Try this code:
Qt Code:
db.setDatabaseName( "full_path_to_db" ); db.setUserName( "UserName" ); db.setPassword( "UserPass" ); if (db.open()) { if (query.exec( "SELECT * FROM tag" )) { while (query.next()) { ui->label->setText( nome ); } query.clear(); } else qDebug() << "Error: " << query.lastError().text(); } else qDebug() << "Error: " << db.lastError().databaseText();To copy to clipboard, switch view to plain text mode
Last edited by Jonny174; 19th April 2012 at 08:15.
ideally, the db file should be stored relative to your executable file. see QCoreApplication::applicationDirPath
qDebug() << query.size() == -1 ? query.numRowsAffected() : query.size();
From documentation: QSqlQuery::size():
Returns the size of the result (number of rows returned), or -1 if the size cannot be determined or if the database does not support reporting information about query sizes.As per quotes above, if everything works but size is -1, then it can't be determined and that's all.From documentation: QSqlQuery::numRowsAffected():
Returns the number of rows affected by the result's SQL statement, or -1 if it cannot be determined.
When working with SQLite, numRowsAffected() nor size() never worked for me.
As long as next() works, you have nothing to worry about.
Hello
I'm trying to find a record in a database through the name. I have the following code:
Qt Code:
//Metodo que procura uma tag através do seu nome QSqlQuery query; query.prepare("SELECT * FROM tag WHERE nome=:nome "); query.bindValue(":nome", nome); query.exec(); qDebug() << query.size(); if(!query.isSelect()){ return true; }else{ return false; } }To copy to clipboard, switch view to plain text mode
However, the size of me and I have always -1 there a record with the same name.
Am I doing something wrong?
Regards,
Daniel Sousa
Please read the post immediately above yours. Only some database engines support reporting the number of rows returned in a query. For Sqlite:
If you only want to know if a record exists then:
Qt Code:
//Metodo que procura uma tag através do seu nome QSqlQuery query; query.prepare("SELECT 1 FROM tag WHERE nome=:nome "); query.bindValue(":nome", nome); return query.exec() && qry.next(); // If the query succeeds and returns a row (or more) then returns true. }To copy to clipboard, switch view to plain text mode
Last edited by ChrisW67; 26th April 2012 at 03:08.
sousadaniel7 (26th April 2012)
Bookmarks