PDA

View Full Version : Sqlite & QPixmap Help please



munna
3rd November 2006, 11:12
Hi,

I am trying to store and retrieve a picture from SQLite database in the following manner but it is not working



//To get the pixmap from database
QPixmap pixmap;

QString statement = "SELECT nameid,photo FROM photo_details WHERE "
"nameid = " + QString::number(currentNameId) + ";";

err = sqlite3_prepare(db,statement.toUtf8().data(), -1, &ppStmt, &tail);
if(err == SQLITE_OK){
while(sqlite3_step(ppStmt) == SQLITE_ROW){
pixmap.loadFromData(QByteArray((const char *)sqlite3_column_blob(ppStmt,1)));
}
}





//To store the picture in the database

QByteArray bytes;
QBuffer buffer(&bytes);
buffer.open(QIODevice::WriteOnly);
pixmap.save(&buffer, "PNG");

int err = 0;
//sqlite3_stmt *ppStmt;
char *errmsg;

sqlite3_stmt *ppStmt;
const char *tail;

statement = "INSERT INTO photo_details VALUES(NULL,?,?);";
err = sqlite3_prepare(db,statement.toUtf8().data(),-1,&ppStmt,&tail);
if(err == SQLITE_OK){
err = sqlite3_bind_int(ppStmt,1,currentNameId);
if(err != SQLITE_OK){
//Error
}
err = sqlite3_bind_blob(ppStmt,2,bytes.constData(),bytes .size(),SQLITE_TRANSIENT);
if(err != SQLITE_OK){

}

if(sqlite3_step(ppStmt) != SQLITE_DONE){
//error
}
}



While storing, the variable err does not throw any error, which means it is stored in the database. But when retrieve the picture back, the pixmap is not build because pixmap.isNull() returns true.

Can someone please tell me, what's wrong with my code ?

Thanks a lot.

jrideout
3rd November 2006, 20:16
I'm not sure on how to use sqlite directly, but if you use Qt's sql classes to handle all this for you, then Qt returns a QVariant::ByteArray for blob data. There are some tricky things with sqilite that Qt handle transparently, so you might want to look into it.

Also, open the database with some sqlite db viewer and make sure that the image is in there, maybe no error was returned, but it wasn't inserted anyway.

munna
4th November 2006, 05:35
I was doing that earlier, but it was very slow. I have other tables also that are part of the application and therefore have to implement everything with QSLite API.

I have successfully implemented everything else with SQLite's API except for this.

Also, I digged into Qt's code and got the above code, but there is something that I am missing.

Probably I should post this on some SQLite forum also.

Thanks a lot.