Results 1 to 12 of 12

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

  1. #1
    Join Date
    Sep 2009
    Posts
    54
    Thanks
    4
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

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

    Dear Friends,
    Now i can store and retrieve sound and image files in local database. now i want to store and retrieve the same in remote sql server database.

    I can store the binary data into remote sql by converting the byteArray to QString while using the insert command. while retrieving i could'nt get original format.
    thanks,
    Askar

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

    thaihoangluu (21st May 2012)

  3. #2
    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

    use base64 for encoding/decoding binary data and then store/restore it in a database.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

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

    thaihoangluu (21st May 2012)

  5. #3
    Join Date
    Sep 2009
    Posts
    54
    Thanks
    4
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 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
    use base64 for encoding/decoding binary data and then store/restore it in a database.
    could you explain with a example code pls

    now i can insert binary data to remote sql server, by storing QByteArray to QString in the insert statement.

    while retrieving from remote sql server it gives binary base64 encoded format data as string, i tried to decode string by using QByteArray::fromBase64(); but i couldnt get that original file
    Last edited by Askar; 14th September 2009 at 11:38.

  6. The following user says thank you to Askar 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

    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
    54
    Thanks
    4
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 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

    I cont apply toBase64 while insert operation ...Qt gives the following runtime error from sql server

    <h1>Url Too Long</h1>

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

    thaihoangluu (21st May 2012)

  11. #6
    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

    I made up a quick dummy snippet which is supposed to load a file from the current directory (some png file) and encode it into databse and reload it from there and save it under another name to the ucrrent direcotry:

    Qt Code:
    1. #include <QtDebug>
    2. #include <QtSql>
    3. #include <QtCore>
    4. #include <QImage>
    5. #include <QApplication>
    6. #include <QWidget>
    7. #include <QVariant>
    8. #include <QByteArray>
    9. #include <QFile>
    10. #include <QBuffer>
    11.  
    12. int main (int argc, char **argv){
    13. QApplication app(argc,argv);
    14. QImage img("./A.png");
    15. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    16. db.setHostName("localhost");
    17. db.setDatabaseName("testdb");
    18. db.setUserName("root");
    19. db.setPassword("abc123"); // not rly
    20. if (!db.open() )
    21. qDebug("Mising db / unable to open");
    22. else {
    23. model.setTable("testtable");
    24.  
    25. int row = 0;
    26. model.insertRows( row, 1);
    27.  
    28. QBuffer buffer(&ba);
    29. buffer.open(QIODevice::WriteOnly);
    30. img.save(&buffer, "PNG");
    31. model.setData(model.index(row,0),ba.toBase64());
    32. model.submitAll();
    33.  
    34. QVariant v(model.data(model.index(row,0)));
    35.  
    36. // this won't work
    37. QFile f("./clone.png");
    38. f.open(QIODevice::ReadWrite | QIODevice::Truncate);
    39. if (!f.error())
    40. {
    41. QByateArray ba2 = v.toByteArray();
    42. ba2.toBase64();
    43. f.write(ba2);
    44. f.close();
    45. }
    46. else
    47. qDebug("unable to open file f");
    48. }
    49. // not really reqired, probably totally useless... indeed useless...
    50. QWidget * a = new QWidget();
    51. app.setApplicationName("halligalli sql");
    52. app.setActiveWindow(a);
    53. a->show();
    54. return app.exec();
    55. }
    To copy to clipboard, switch view to plain text mode 

    Someone can tell me wehere my mistake is?

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

    thaihoangluu (21st May 2012)

  13. #7
    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].

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

    thaihoangluu (21st May 2012)

  15. #8
    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

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

    thaihoangluu (21st May 2012)

  17. #9
    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

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

    thaihoangluu (21st May 2012)

  19. #10
    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].

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

    thaihoangluu (21st May 2012)

  21. #11
    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...

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

    thaihoangluu (21st May 2012)

  23. #12
    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.