Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to read and write data in Visual FoxPro files in Windows-1251.



  • Hi all,
    I use Qt5.3 for Windows 7 64bit. I try to read and write data in Visual FoxPro files in Windows-1251 but Cyrillic characters display as gibberish (????). That is my code:
    @
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("Windows-1251"));

    QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
    db.setDatabaseName("DRIVER={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=C:/database;Exclusive=No;NULL=NO;Collate=Machine;BACKGROUNDFETCH=NO;DELETED=NO;");
    db.open();

    QSqlQuery query;
    query.exec("SELECT * FROM sale");

    while (query.next())
    {
    QString code = query.value(0).toByteArray();
    QString name = query.value(1).toByteArray();
    QString price = query.value(2).toByteArray();
    qDebug() << code << name << price;
    }
    @

    or

    @
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("Windows-1251"));

    QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
    db.setDatabaseName("DRIVER={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=C:/database;Exclusive=No;NULL=NO;Collate=Machine;BACKGROUNDFETCH=NO;DELETED=NO;");
    db.open();

    QSqlQuery query;
    query.exec("SELECT * FROM sale");

    QTextCodec * codec = QTextCodec::codecForName("Windows-1251");
    while (query.next())
    {
    QString code = codec->toUnicode(query.value(0).toByteArray());
    QString name = codec->toUnicode(query.value(1).toByteArray());
    QString price = codec->toUnicode(query.value(2).toByteArray());
    qDebug() << code << name << price;
    }
    @
    Why does it happen? Can you give me some advice on how to decide my question?


Log in to reply