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.

    So:

    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

    Hi,

    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");
        db.setDatabaseName(nombre);
        db.open();
    
        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);
        crear.prepare(consulta);
        crear.exec();
    

    the insert of data:

        QString agregar;
        agregar.append( "INSERT INTO semaforos("
                        "a,"
                        "b,"
                        "c,"
                        "d,"
                        "e,"
                        "f,"
                        "g,"
                        "h,"
                        "i,"
                        "j"") "
                        "VALUES("
                        "'"+ui->lineEdit_interseccion->text()+"',"
                        "'"+ui->lineEdit_marca->text()+"',"
                         "'"+ui->lineEdit_caracteristica->text()+"',"
                         "'"+ui->lineEdit_modelo->text()+"',"
                         "'"+ui->lineEdit_tiempo->text()+"',"
                         "'"+ui->lineEdit_peatonal->text()+"',"
                         "'"+ui->lineEdit_tecnologia->text()+"',"
                         "'"+ui->lineEdit_cantidad->text()+"',"
                         "'"+ui->lineEdit_descripcion->text()+"',"
                        "'"+ui->textEdit->toPlainText()+"'"
                        ");"      );
    
    
        QSqlQuery insertar(db);
        insertar.prepare(agregar);
        insertar.exec();
    

    Reading data to compare.

        QSqlQuery insertar(db);
        insertar.prepare("SELECT * FROM semaforos");
        insertar.exec();
        int i=0;
        if(insertar.value(2)!=ui->lineEdit_caracteristica->text())
        {
            i=1;
        }
    
    

    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
 

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