PDA

View Full Version : do you know how to check if a record exist in qt ?..



firesh
2nd October 2010, 08:53
do you know how to check if a record exist in qt ?..

what i would like to do is .. i listing all the members in a drop box which the names are from across 2 database tables ...

so i would like to check if this record exist in the first table .. else check in table 2.. then proceed to do something..

but i am stuck on how to check if a record exist in qt.. i am using sqlite.

ChrisW67
2nd October 2010, 22:11
You execute a query looking for the record and see if it returns any rows. There is nothing Qt-specific about this.

bool ok;
bool foundBoth = false;
QSqlQuery q;
ok = q.exec("select 1 from table1 where field = value");
if (ok && q.next()) {
ok = q.exec("select 1 from table2 where field = value");
if (ok && q.next()) {
foundBoth = true;
}
}

Use bound parameters if there is any chance the value can be supplied directly by the user or contain semicolons, quotes etc. Heck, just use bound parameters anyway.