dsp-site/wiki/Prog/Lang/CPP/Qt/Qt обмен бинарными данными с БД.adoc
2019-07-15 21:47:09 +03:00

42 lines
1.0 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

= Qt: обмен бинарными данными с БД
:title-separator: {sp}|
:category: Программирование
:tags: программирование, qt, postgresql, бд
Таблица, с которой осуществляется обмен:
[source,sql]
----
CREATE TABLE example (
id INTEGER,
bin_data BYTEA
);
----
Запись данных:
[source,cpp]
----
const char cart[] = {0x04, 0x43, 0x00, 0x9A};
QByteArray binDataArray(QByteArray::fromRawData(cart, 4));
QSqlField dataField("bin_data", QVariant::ByteArray);
byteField.setValue(binDataArray);
QSqlDatabase db = QSqlDatabase::database();
QSqlQuery query;
query.exec(QString("INSERT INTO files VALUES(%1, %2);")
.arg(18)
.arg(db.driver()->formatValue(dataField)));
----
Чтение данных:
[source,cpp]
----
QSqlQuery query;
query.exec("SELECT id, bin_data FROM example LIMIT 1");
query.next();
QByteArray binDataArray = query.value(query.record().indexOf("bin_data")).toByteArray();
----