2019-06-20 03:56:39 +00:00
|
|
|
|
= 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;
|
2019-08-29 14:19:41 +00:00
|
|
|
|
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();
|
|
|
|
|
----
|
2019-06-20 03:56:39 +00:00
|
|
|
|
|
|
|
|
|
Чтение данных:
|
|
|
|
|
|
|
|
|
|
[source,cpp]
|
2019-08-29 14:19:41 +00:00
|
|
|
|
----
|
2019-06-20 03:56:39 +00:00
|
|
|
|
QSqlQuery query;
|
|
|
|
|
query.exec("SELECT id, bin_data FROM example LIMIT 1");
|
|
|
|
|
query.next();
|
|
|
|
|
QByteArray binDataArray = query.value(query.record().indexOf("bin_data")).toByteArray();
|
2019-08-29 14:19:41 +00:00
|
|
|
|
----
|
2019-06-20 03:56:39 +00:00
|
|
|
|
|