Results 1 to 12 of 12

Thread: How can i store and retrive image / sound files to and from Remaote sql server

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How can i store and retrive image / sound files to and from Remaote sql server

    try this example, works fine for me.
    Qt Code:
    1. #include <QtGui>
    2. #include <QtSql>
    3. #include <QApplication>
    4.  
    5. int main(int argc, char **argv)
    6. {
    7. QApplication app(argc, argv);
    8.  
    9. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    10. db.setDatabaseName("pictures");
    11. if (!db.open())
    12. return db.lastError().number();
    13.  
    14. QSqlQuery query;
    15. qDebug() << query.exec("CREATE TABLE IF NOT EXISTS pictures (id INT PRIMARY KEY, pic BLOB)");
    16.  
    17. QImage image("flower.jpg");
    18. qDebug() << image.size();
    19. QByteArray byteArray;
    20. QBuffer buffer(&byteArray);
    21. buffer.open(QIODevice::WriteOnly);
    22. image.save(&buffer, "jpg");
    23.  
    24. query.prepare("INSERT INTO pictures (id, pic) VALUES (?, ?)");
    25. query.addBindValue(1);
    26. query.addBindValue(byteArray);
    27. qDebug() << query.exec();
    28.  
    29. qDebug() << query.exec("SELECT pic FROM pictures WHERE id = 1");
    30. qDebug() << query.next();
    31.  
    32. byteArray = query.value(0).toByteArray();
    33. QPixmap pixmap = QPixmap::fromImage(QImage::fromData(byteArray, "jpg"));
    34. qDebug() << pixmap.size();
    35.  
    36. QLabel label;
    37. label.setPixmap(pixmap);
    38. label.show();
    39.  
    40. return app.exec();
    41. }
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  2. The following user says thank you to spirit for this useful post:

    thaihoangluu (21st May 2012)

  3. #2
    Join Date
    Sep 2009
    Posts
    36
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How can i store and retrive image / sound files to and from Remaote sql server

    Found my mistake, first row within my table had zer0 (NULL) in the blob field :-S .. dumb

  4. The following user says thank you to soxs060389 for this useful post:

    thaihoangluu (21st May 2012)

  5. #3
    Join Date
    Sep 2009
    Posts
    36
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How can i store and retrive image / sound files to and from Remaote sql server

    And what was all the mess about toBase64() and fromBase64().

    if I use that, I can't recreate the image from db?

    Greets

  6. The following user says thank you to soxs060389 for this useful post:

    thaihoangluu (21st May 2012)

  7. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How can i store and retrive image / sound files to and from Remaote sql server

    try my example, it stores to database and recreates an image from database.
    you don't need to use base64 at all. it was my mistake, I thought that you need to store an xml with an image into databse.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  8. The following user says thank you to spirit for this useful post:

    thaihoangluu (21st May 2012)

  9. #5
    Join Date
    Sep 2009
    Posts
    36
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Talking Re: How can i store and retrive image / sound files to and from Remaote sql server

    oh, ok... you may be forgiven.. things just happen.

    So my code works too, (just drop the *Base64 stuff) and I prefer at as I disklike SQL syntax blah... ;-)

    Note: no my program code is not cluttered as the example ;-) it was just rapid typing...

  10. The following user says thank you to soxs060389 for this useful post:

    thaihoangluu (21st May 2012)

  11. #6
    Join Date
    Dec 2011
    Posts
    33
    Thanks
    56
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How can i store and retrive image / sound files to and from Remaote sql server

    Quote Originally Posted by spirit View Post
    try this example, works fine for me.
    Qt Code:
    1. #include <QtGui>
    2. #include <QtSql>
    3. #include <QApplication>
    4.  
    5. int main(int argc, char **argv)
    6. {
    7. QApplication app(argc, argv);
    8.  
    9. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    10. db.setDatabaseName("pictures");
    11. if (!db.open())
    12. return db.lastError().number();
    13.  
    14. QSqlQuery query;
    15. qDebug() << query.exec("CREATE TABLE IF NOT EXISTS pictures (id INT PRIMARY KEY, pic BLOB)");
    16.  
    17. QImage image("flower.jpg");
    18. qDebug() << image.size();
    19. QByteArray byteArray;
    20. QBuffer buffer(&byteArray);
    21. buffer.open(QIODevice::WriteOnly);
    22. image.save(&buffer, "jpg");
    23.  
    24. query.prepare("INSERT INTO pictures (id, pic) VALUES (?, ?)");
    25. query.addBindValue(1);
    26. query.addBindValue(byteArray);
    27. qDebug() << query.exec();
    28.  
    29. qDebug() << query.exec("SELECT pic FROM pictures WHERE id = 1");
    30. qDebug() << query.next();
    31.  
    32. byteArray = query.value(0).toByteArray();
    33. QPixmap pixmap = QPixmap::fromImage(QImage::fromData(byteArray, "jpg"));
    34. qDebug() << pixmap.size();
    35.  
    36. QLabel label;
    37. label.setPixmap(pixmap);
    38. label.show();
    39.  
    40. return app.exec();
    41. }
    To copy to clipboard, switch view to plain text mode 

    oh great...thanks so much...
    Nguyễn Lưu Vũ - Http://nguyenluuvu.com

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
  •  
Qt is a trademark of The Qt Company.