Important: Please read the Qt Code of Conduct -

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( != "".

    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");
        Semana << "LUNES" << "MARTES" << "MIERCOLES" << "JUEVES" << "VIERNES" << "SÁBADO";
            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()<<"Record del query1"<<query1.record().indexOf(;
                    qDebug()<<"i es:"<<i;
                    if(query1.value(query1.record().indexOf( != ""){
                        Dias << Semana[i];                   
                        if(c == ""){
                          c=query1.value(query1.record().indexOf(Semana[i])).toString()+". ";
                        c=c+query1.value(query1.record().indexOf(Semana[i])).toString()+". ";

  • Lifetime Qt Champion

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

    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:; //In 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