0

I have a such problem: I develop a simple database (PostgreSql). One of the features that it keeps in column (column name: "ascii") a big text data (2000 rows) that reads from file (datatype in the table is BYTEA). Here is the example of the code where I insert the data:

QFile *asciiFile = new QFile(ui->ascii_lineEdit->text());
    asciiFile->open(QIODevice::ReadOnly);
    QByteArray asciiArray = asciiFile->readAll();
    qDebug() << asciiArray;

    QSqlQuery *sourceQuery = new QSqlQuery();
    sourceQuery->prepare("INSERT INTO source (image, image_material, archive, ascii, about) VALUES (:image, :image_material, :archive, :ascii, :about)");
    sourceQuery->bindValue(":image", graphArray.toBase64());
    sourceQuery->bindValue(":image_material", materialArray.toBase64());
    sourceQuery->bindValue(":archive", archiveArray.toBase64());
    sourceQuery->bindValue(":ascii", asciiArray.toBase64());
    sourceQuery->bindValue(":about", ui->about_textEdit->toPlainText());
    sourceQuery->exec();

Once more, the datatype of the column("ascii") that keeps is BYTEA. Now I try to read this data (near 2000 lines) and they not displayed as well. I try this method:

QByteArray asciiArray = QSqlQuery query = connector->getSourceAscii(item_id);
    query.next();
    QByteArray asciiArray = QByteArray::fromBase64(query.value("ascii").toByteArray());
    QString *result = new QString(asciiArray);
    qDebug() << *result;

It must be a lines of the digits but I have something like this: �w߇�k��ۇ������z���������燸��wӇ�k�{�����w۝��};��{��x{��k�{k�{k�xk�x߾���{k�{�����������wㇸ��


The examples of the lines that I need:
2490 0,21979421377182 4,82690520584583E-02
2491 0,226718083024025 4,33071963489056E-02

Have you any suggestion about this? Thanks.

lazexe
  • 367
  • 4
  • 19
  • 1
    `QString` will convert the `asciiArray` to UTF-16 by assuming it's UTF-8 - which is probably isn't... Just print out the `asciiArray` directly by using the `constData()` method. And why are you declaring a `QString` on the heap? – cmannett85 Jul 29 '15 at 08:55
  • Try `QSqlQuery("SET bytea_output=escape;", db);` right after opening database. – Amartel Jul 29 '15 at 08:56
  • Thanks for answer but any of methods above didn't helped. – lazexe Jul 29 '15 at 09:06
  • You can try `QString str = QString::fromUtf16(asciiArray.data());` or `QString str = QString::fromLatin1(asciiArray.data());` or any other from* function depending of the format of the strings you have in database – johngull Jul 29 '15 at 09:19

0 Answers0