dsp-site/wiki/Prog/Lang/CPP/Qt/Qt обмен бинарными данными с БД.adoc
2019-08-29 17:19:41 +03:00

39 lines
952 B
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));
QSqlQuery query;
query.prepare(QString("INSERT INTO example (id, bin_data) VALUES(:id, :bin_data)");
query.bindValue(":id", 10, QSql::In);
query.bindValue(":bin_data", binDataArray, QSql::In | QSql::Binary);
query.exec();
----
Чтение данных:
[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();
----