Send QString with Tildes and Accent to MySQL?



  • 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()+". ";
                        }
                    }
                }      
            }
        }
    

  • Moderators

    @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?



  • @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;



  • 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.



  • @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.


Log in to reply
 

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