PDA

View Full Version : How to update BLOB field in a SQLite database ?



aircraftstories
8th April 2011, 13:04
Hello,

I would like to update a BLOB field in my database with a QByteArray, but it doesn't work. Can you help me ?

Ma database structure is :

CREATE TABLE Settings (
Version nvarchar(10),
Geometry BLOB
);

This database have 1 row :

INSERT INTO Settings VALUES (
'0.1',
''
);

and I would like to update this row with the following source code :

// connection :
< database connection code >

// update :
QByteArray a_value;
a_value = saveGeometry(); // saveGeometry return a QByteArray

QSqlQuery query;
query.prepare("UPDATE Settings SET Geometry = ':Geometry'");
query.bindValue(":Geometry", a_value);
if (query.exec() == false)
qDebug() << "Error SQL exec()";


The result is : Error SQL exec()

Thanks

Rhayader
8th April 2011, 13:08
if you pass query.lastError().text() to qDebug() you will get the specific error info

stampede
8th April 2011, 13:16
Try without the quotes:


query.prepare("UPDATE Settings SET Geometry = :Geometry");

aircraftstories
8th April 2011, 13:36
Try without the quotes:


Perfect, it works now. Thank you.

The next problem is that the QByteArray returned by the function SaveGeometry() can have the following values :

QByteArray a_value;
a_value = saveGeometry();
a_value[0] = 79
a_value[1] = 28
a_value[2] = 41
a_value[3] = 00
a_value[4] = 27
.......
a_value[X] = YY

And in my BLOB cell only "79 28 41" is stored. the value "a_value[3] = 00" is considered as a '\0' end of string .... any idea to store the complete QByteArray ?

Thanks

stampede
8th April 2011, 14:20
Show the code where you save and read from db. This works ok:


#include <QApplication>
#include <QtCore>
#include <QtSql>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QByteArray bytes;
bytes.resize(6);
bytes[0] = 79;
bytes[1] = 28;
bytes[2] = 41;
bytes[3] = 00;
bytes[4] = 27;
bytes[5] = 11;

qDebug() << "bytes size: " << bytes.size();

QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("testdb");
db.open();
QSqlQuery query(db);
query.exec("DROP TABLE test;");
query.exec("CREATE TABLE test( data BLOB );");
query.prepare("INSERT INTO test VALUES(:data);");
query.bindValue(":data",bytes);
query.exec();

query.exec("SELECT data FROM test;");
if( query.next() ){
QByteArray data = query.value(0).toByteArray();
qDebug() << "from db size: " << data.size();
qDebug() << "equal?" << (data==bytes);
}

return 0;
}

Output:


bytes size: 6
from db size: 6
equal? true

JohannesMunk
8th April 2011, 14:22
Are you sure? How are you checking that? Maybe the program your are viewing the blob with does interpret it that way. Try reading it back from the db the same way and dump the bytearray to your console.

Ah: stampede was faster!

Joh

aircraftstories
8th April 2011, 20:45
Are you sure? How are you checking that? Maybe the program your are viewing the blob with does interpret it that way.

Thank you guys ! it was the problem. Now, all works.