Important: Please read the Qt Code of Conduct -

Problem with a comparison between a QString and a TEXT in SQLite's database

  • Hello, I have to compare a QString which it's from a lineEdit with a TEXT value in a SQLite databe.

    If I see:

    QVariant QSqlQuery::value(int index) const.


    if(querry.value(0) != ui->lineEdiut->text()) 
    //do something

    But no matter what chain put in the database is always different the comparison. And what is in the querry.value(0) I compared with SQLite Expert and the texts are the same. So I guess that the comparison is not by difference of variable types.

  • Lifetime Qt Champion


    Can you show how you are creating and using your query before that if ?

  • the definition of table:

        QSqlDatabase db;
        db= QSqlDatabase::addDatabase("QSQLITE", "first_connection");
        QString consulta;
        consulta.append("CREATE TABLE IF NOT EXISTS semaforos("
                        "a TEXT primary key,"
                        "b TEXT,"
                        "c TEXT,"
                        "d TEXT,"
                        "e TEXT,"
                        "f TEXT,"
                        "g TEXT,"
                        "h TEXT,"
                        "i TEXT,"
                        "j TEXT"
        QSqlQuery crear(db);

    the insert of data:

        QString agregar;
        agregar.append( "INSERT INTO semaforos("
                        "j"") "
                        ");"      );
        QSqlQuery insertar(db);

    Reading data to compare.

        QSqlQuery insertar(db);
        insertar.prepare("SELECT * FROM semaforos");
        int i=0;

    and i it's not 1 even if I see that the string are the same.

  • Lifetime Qt Champion

    You query is currently position on an invalid record like explained in the QSqlQuery::exec documentation. You first have to call next and then continue with your tests.

  • you are the best!

Log in to reply