arthur
arthur

Reputation: 1064

insert array (binary data) into a postgres DB with qsql (c++ and qt)

Consider the following interraction with the postgres database:

QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
db.setHostName("acidalia");
db.setDatabaseName("customdb");
db.setUserName("mojito");
db.setPassword("J0a1m8");
bool ok = db.open();
QSqlQuery query(db);

QSqlQuery query(db);

QVector<int> byteArray(2); 
byteArray[0] = 0;
byteArray[1] = 7;

QVariant v = QVariant::fromValue(byteArray); 

cout << "dropping a table: " << query.exec("drop table aaa;") << endl; //gives 1
cout << "creating a table: " << query.exec("create table aaa (gid integer, pos integer[])") << endl; // gives 0
query.prepare("INSERT INTO aaa (gid) VALUES (:gid, :pos)");
query.bindValue(0, 1);
query.bindValue(1, v);
cout << "inserting: " << query.exec() << endl; // gives 0 :-(

Of course, one way to do that would be to send the data with a manually built sql statement, and execute the query as a normal query on the server (where the byte array would be inserted as a string), but I am looking for a somewhat nicer solution..

Upvotes: 0

Views: 2301

Answers (1)

Daniel V&#233;rit&#233;
Daniel V&#233;rit&#233;

Reputation: 61556

The INSERT has 3 destination columns declared but 4 bind values.

query.prepare("INSERT INTO geo (gid, bboxx, bboxy) " "VALUES (:gid, :bboxx, :bboxy, :pos)");

Once the bytea column is added after bboxy, this should work.

Upvotes: 1

Related Questions