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. Send QString with Tildes and Accent to MySQL?
Forum Updated to NodeBB v4.3 + New Features

Send QString with Tildes and Accent to MySQL?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 1.7k 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.
  • E Offline
    E Offline
    Eduardo12l
    wrote on last edited by Eduardo12l
    #1

    My MySQL Database table contains days from Monday to Saturday (in spanish) where each day is a field (column). I'm trying to send a QString of my container Semana[i] in order to get data to use this:

    query.value(query.record().indexOf(Semana.at(i))).toString() != "".
    
    

    IndexOf() needs a QString because it needs to find coincidence, however, when Semana[i] = "SÁBADO" //Contains Á my Qt program fails in finding coincidences, i'm quite certain that it happends because it has a character with accent (or diacritic) that Qt when tries to connect with MySQL corrupts. When I use qDebug()<<query1.record().fieldName(int x); I get SÃ\u0081BADO.

    This is my code:

    void Ventanas0::on_pushButton_clicked()
    {
        QSqlQuery query1 (QSqlDatabase::database("Conexion1"));
        query1.prepare("SELECT * FROM año2017");
        query1.first();
        query1.exec();
        Semana << "LUNES" << "MARTES" << "MIERCOLES" << "JUEVES" << "VIERNES" << "SÁBADO";
        qDebug()<<Semana;
        while(query1.next()){
            z = false;
            a = query1.value(query1.record().indexOf("NOMBRE")).toString();
            d = query1.value(query1.record().indexOf("GPROF")).toString();
            a = a.toLocal8Bit(); 
            consulta = a+" "+"("+d+")";
            if(ui->comboBox->currentText() == consulta){
                z = true;
            }else if(ui->comboBox_2->currentText()== consulta){
                z = true;
            }else if(ui->comboBox_3->currentText()== consulta){
                z = true;
            }else if(ui->comboBox_4->currentText()== consulta){
                z = true;
            }else if(ui->comboBox_5->currentText()== consulta){
                z = true;
            }else if(ui->comboBox_6->currentText()== consulta){
                z = true;
            }else if(ui->comboBox_7->currentText()== consulta){
                z = true;
            }else if(ui->comboBox_8->currentText()== consulta){
                z = true;
            }else if(ui->comboBox_9->currentText()== consulta){
                z = true;
            }else if(ui->comboBox_10->currentText()== consulta){
                z = true;
            }else if(ui->comboBox_11->currentText()== consulta){
                z = true;
            }else if(ui->comboBox_12->currentText()== consulta){
                z = true;
            }else if(ui->comboBox_13->currentText()== consulta){
                z = true;
            }else if(ui->comboBox_14->currentText()== consulta){
                z = true;
            }else if(ui->comboBox_15->currentText()== consulta){
                z = true;
            }else if(ui->comboBox_16->currentText()== consulta){
                z = true;
            }else if(ui->comboBox_17->currentText()== consulta){
                z = true;
            }else if(ui->comboBox_18->currentText()== consulta){
                z = true;
            }else if(ui->comboBox_19->currentText()== consulta){
                z = true;
            }else if(ui->comboBox_20->currentText()== consulta){
                z = true;
            }else if(ui->comboBox_21->currentText()== consulta){
                z = true;
            }else if(ui->comboBox_22->currentText()== consulta){
                z = true;
            }else if(ui->comboBox_23->currentText()== consulta){
                z = true;
            }else if(ui->comboBox_24->currentText()== consulta){
                z = true;
            }
            c = "";
            if(z == true){ 
                for(int i = 0; i < Semana.size(); i++){
                    qDebug()<<query1.record().fieldName(x); 
                    qDebug()<<"Record del query1"<<query1.record().indexOf(Semana.at(i));
                    qDebug()<<"i es:"<<i;
                    qDebug()<<"Semana.at(i)"<<Semana.at(i);
                    qDebug()<<"query1.value(query1.record().indexOf(Semana[i])).toString()"<<query1.value(query1.record().indexOf(Semana.at(i))).toString();         
                    if(query1.value(query1.record().indexOf(Semana.at(i))).toString() != ""){
                        Dias << Semana[i];                   
                        if(c == ""){
                          c=query1.value(query1.record().indexOf(Semana[i])).toString()+". ";
                        }else{
                        c=c+query1.value(query1.record().indexOf(Semana[i])).toString()+". ";
                        }
                    }
                }      
            }
        }
    
    jsulmJ 1 Reply Last reply
    0
    • E Eduardo12l

      My MySQL Database table contains days from Monday to Saturday (in spanish) where each day is a field (column). I'm trying to send a QString of my container Semana[i] in order to get data to use this:

      query.value(query.record().indexOf(Semana.at(i))).toString() != "".
      
      

      IndexOf() needs a QString because it needs to find coincidence, however, when Semana[i] = "SÁBADO" //Contains Á my Qt program fails in finding coincidences, i'm quite certain that it happends because it has a character with accent (or diacritic) that Qt when tries to connect with MySQL corrupts. When I use qDebug()<<query1.record().fieldName(int x); I get SÃ\u0081BADO.

      This is my code:

      void Ventanas0::on_pushButton_clicked()
      {
          QSqlQuery query1 (QSqlDatabase::database("Conexion1"));
          query1.prepare("SELECT * FROM año2017");
          query1.first();
          query1.exec();
          Semana << "LUNES" << "MARTES" << "MIERCOLES" << "JUEVES" << "VIERNES" << "SÁBADO";
          qDebug()<<Semana;
          while(query1.next()){
              z = false;
              a = query1.value(query1.record().indexOf("NOMBRE")).toString();
              d = query1.value(query1.record().indexOf("GPROF")).toString();
              a = a.toLocal8Bit(); 
              consulta = a+" "+"("+d+")";
              if(ui->comboBox->currentText() == consulta){
                  z = true;
              }else if(ui->comboBox_2->currentText()== consulta){
                  z = true;
              }else if(ui->comboBox_3->currentText()== consulta){
                  z = true;
              }else if(ui->comboBox_4->currentText()== consulta){
                  z = true;
              }else if(ui->comboBox_5->currentText()== consulta){
                  z = true;
              }else if(ui->comboBox_6->currentText()== consulta){
                  z = true;
              }else if(ui->comboBox_7->currentText()== consulta){
                  z = true;
              }else if(ui->comboBox_8->currentText()== consulta){
                  z = true;
              }else if(ui->comboBox_9->currentText()== consulta){
                  z = true;
              }else if(ui->comboBox_10->currentText()== consulta){
                  z = true;
              }else if(ui->comboBox_11->currentText()== consulta){
                  z = true;
              }else if(ui->comboBox_12->currentText()== consulta){
                  z = true;
              }else if(ui->comboBox_13->currentText()== consulta){
                  z = true;
              }else if(ui->comboBox_14->currentText()== consulta){
                  z = true;
              }else if(ui->comboBox_15->currentText()== consulta){
                  z = true;
              }else if(ui->comboBox_16->currentText()== consulta){
                  z = true;
              }else if(ui->comboBox_17->currentText()== consulta){
                  z = true;
              }else if(ui->comboBox_18->currentText()== consulta){
                  z = true;
              }else if(ui->comboBox_19->currentText()== consulta){
                  z = true;
              }else if(ui->comboBox_20->currentText()== consulta){
                  z = true;
              }else if(ui->comboBox_21->currentText()== consulta){
                  z = true;
              }else if(ui->comboBox_22->currentText()== consulta){
                  z = true;
              }else if(ui->comboBox_23->currentText()== consulta){
                  z = true;
              }else if(ui->comboBox_24->currentText()== consulta){
                  z = true;
              }
              c = "";
              if(z == true){ 
                  for(int i = 0; i < Semana.size(); i++){
                      qDebug()<<query1.record().fieldName(x); 
                      qDebug()<<"Record del query1"<<query1.record().indexOf(Semana.at(i));
                      qDebug()<<"i es:"<<i;
                      qDebug()<<"Semana.at(i)"<<Semana.at(i);
                      qDebug()<<"query1.value(query1.record().indexOf(Semana[i])).toString()"<<query1.value(query1.record().indexOf(Semana.at(i))).toString();         
                      if(query1.value(query1.record().indexOf(Semana.at(i))).toString() != ""){
                          Dias << Semana[i];                   
                          if(c == ""){
                            c=query1.value(query1.record().indexOf(Semana[i])).toString()+". ";
                          }else{
                          c=c+query1.value(query1.record().indexOf(Semana[i])).toString()+". ";
                          }
                      }
                  }      
              }
          }
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @Eduardo12l QString uses UTF-16 internally. Which encoding do you use in your database? Did you verified that the data was stored correctly in the db? Do you write the data into your db from your Qt app?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      E 1 Reply Last reply
      1
      • Vinod KuntojiV Offline
        Vinod KuntojiV Offline
        Vinod Kuntoji
        wrote on last edited by
        #3

        @Eduardo12l said in Send QString with Tildes and Accent to MySQL?:

        Semana.at(i)

        I am getting the correct string. Use utf-8 encoding.
        QString Semana = "SÁBADO";
        QString str = Semana.toUtf8();
        qDebug() << str << endl;

        C++, Qt, Qt Quick Developer,
        PthinkS, Bangalore

        E 1 Reply Last reply
        0
        • jsulmJ jsulm

          @Eduardo12l QString uses UTF-16 internally. Which encoding do you use in your database? Did you verified that the data was stored correctly in the db? Do you write the data into your db from your Qt app?

          E Offline
          E Offline
          Eduardo12l
          wrote on last edited by Eduardo12l
          #4

          Hi @jsulm .

          • I used UTF-8.

          • The values in my db are well-written.

          • I exported the contents from an Excel worksheet to MySQL db. I used the special software that is in MySQL website. The Excel worksheet was created by someone else.

          I used:

          Semana.at(5).toUtf8(); //In Semana.at(5) is "SÁBADO"
          

          But the problem continues.

          1 Reply Last reply
          0
          • Vinod KuntojiV Vinod Kuntoji

            @Eduardo12l said in Send QString with Tildes and Accent to MySQL?:

            Semana.at(i)

            I am getting the correct string. Use utf-8 encoding.
            QString Semana = "SÁBADO";
            QString str = Semana.toUtf8();
            qDebug() << str << endl;

            E Offline
            E Offline
            Eduardo12l
            wrote on last edited by
            #5

            @Vinod-Kuntoji I think the problem happends when Qt program sends the data to MySQL. I have no problem with typical english characters but with acents and Ñs is another story.

            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