mardi 15 décembre 2015

Qt: How to check if the entered Primary-key exist in SQLite database or not?

I am trying to check if the primary-key (entered manually by the user) exists already in the SQLite database or not (in order to decide whether to proceed with an insertion of a new record or to do an update for an existent one).

I've tried:

  • query.exce();
  • query.isEmpty();
  • and bellow I'm trying: guery.isNull();

However, they all give me the same result: they all say the record doesn't exist (return 0) and go to the insertion function. They return 0 even if the ref_no does exist).

Here is my code for isNull() function:

int DatabaseManager::checkRefNoExist(QString ref_no){
    QSqlQuery query;
    query.prepare("SELECT * FROM basic_info WHERE ref_no = :ref_no");
    query.bindValue(":ref_no", ref_no);
    query.exec();

    if(query.isNull(ref_no.toInt())){
        return 0; // whatever the ref_no is, it always comes here !!
    } else {
        return 1;
    }
}

Aucun commentaire:

Enregistrer un commentaire