Results 1 to 3 of 3

Thread: Sqlite & QPixmap Help please

  1. #1
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Sqlite & QPixmap Help please

    Hi,

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

    Qt Code:
    1. //To get the pixmap from database
    2. QPixmap pixmap;
    3.  
    4. QString statement = "SELECT nameid,photo FROM photo_details WHERE "
    5. "nameid = " + QString::number(currentNameId) + ";";
    6.  
    7. err = sqlite3_prepare(db,statement.toUtf8().data(), -1, &ppStmt, &tail);
    8. if(err == SQLITE_OK){
    9. while(sqlite3_step(ppStmt) == SQLITE_ROW){
    10. pixmap.loadFromData(QByteArray((const char *)sqlite3_column_blob(ppStmt,1)));
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //To store the picture in the database
    2.  
    3. QByteArray bytes;
    4. QBuffer buffer(&bytes);
    5. buffer.open(QIODevice::WriteOnly);
    6. pixmap.save(&buffer, "PNG");
    7.  
    8. int err = 0;
    9. //sqlite3_stmt *ppStmt;
    10. char *errmsg;
    11.  
    12. sqlite3_stmt *ppStmt;
    13. const char *tail;
    14.  
    15. statement = "INSERT INTO photo_details VALUES(NULL,?,?);";
    16. err = sqlite3_prepare(db,statement.toUtf8().data(),-1,&ppStmt,&tail);
    17. if(err == SQLITE_OK){
    18. err = sqlite3_bind_int(ppStmt,1,currentNameId);
    19. if(err != SQLITE_OK){
    20. //Error
    21. }
    22. err = sqlite3_bind_blob(ppStmt,2,bytes.constData(),bytes.size(),SQLITE_TRANSIENT);
    23. if(err != SQLITE_OK){
    24.  
    25. }
    26.  
    27. if(sqlite3_step(ppStmt) != SQLITE_DONE){
    28. //error
    29. }
    30. }
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Feb 2006
    Location
    Boulder, Colorado, USA
    Posts
    63
    Thanked 8 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sqlite & QPixmap Help please

    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.

  3. #3
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sqlite & QPixmap Help please

    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.

Similar Threads

  1. Convert QPixmap to QByteArray ?
    By probine in forum Qt Programming
    Replies: 5
    Last Post: 13th March 2014, 08:23
  2. QPixmap into QTextBrowser
    By xgoan in forum Qt Programming
    Replies: 6
    Last Post: 3rd November 2006, 13:05
  3. QPixmap -> HICON trouble.
    By krivenok in forum Qt Programming
    Replies: 1
    Last Post: 11th August 2006, 15:51
  4. Loading a custom image into a QPixmap
    By KShots in forum Qt Programming
    Replies: 12
    Last Post: 5th August 2006, 00:16
  5. QPixmap and HBITMAP
    By ToddAtWSU in forum Qt Programming
    Replies: 1
    Last Post: 21st June 2006, 16:24

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.