Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem with a comparison between a QString and a TEXT in SQLite's database

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

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • JulianJ Offline
    JulianJ Offline
    Julian
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • JulianJ Offline
        JulianJ Offline
        Julian
        wrote on last edited by Julian
        #3

        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.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • JulianJ Offline
            JulianJ Offline
            Julian
            wrote on last edited by
            #5

            you are the best!

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved