Results 1 to 5 of 5

Thread: Inserting blob into mysql using QT?

  1. #1
    Join Date
    May 2007
    Location
    Lublin, Poland
    Posts
    345
    Thanks
    40
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Inserting blob into mysql using QT?

    Hi,

    I am not so strong in mysql/sql and have this, hopefully, easy question:

    How to store/read a .png file in mysql database using Qt.
    I have a table containing 2 columns: snap_time,snap; (datetime,blob)

    I tried this, but I get an error:

    Qt Code:
    1. QSqlQuery query;
    2. QByteArray bArray;
    3. QBuffer buffer(&bArray);
    4. buffer.open(QIODevice::WriteOnly);
    5. QDataStream out(&buffer);
    6. out << _pixmap.toImage();
    7. buffer.close();
    8. query.prepare("insert into memos(snap,snap_time) values (:snap, :time)");
    9. query.bindValue(":snap",bArray->constData());
    10. query.bindValue(":time",_time.toString("yyyy-MM-dd hh:mm:ss"));
    11. if(!query.exec())
    12. QMessageBox::warning(0,"Error inserting snap","SNAP");
    To copy to clipboard, switch view to plain text mode 

    Thank you for help
    Qt allows you to use everything you want
    wysota
    --------------------------------------------------------------------------------
    #if defined(Q_OS_UNIX) && defined(QT_DEBUG)
    abort(); // trap; generates core dump
    #else
    exit(1); // goodbye cruel world
    #endif

  2. #2
    Join Date
    May 2007
    Location
    Lublin, Poland
    Posts
    345
    Thanks
    40
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Inserting blob into mysql using QT?

    Anyone?

    Kacper
    Qt allows you to use everything you want
    wysota
    --------------------------------------------------------------------------------
    #if defined(Q_OS_UNIX) && defined(QT_DEBUG)
    abort(); // trap; generates core dump
    #else
    exit(1); // goodbye cruel world
    #endif

  3. #3
    Join Date
    Apr 2010
    Location
    Singapore
    Posts
    156
    Thanks
    47
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Inserting blob into mysql using QT?

    can you change line 10 to :
    Qt Code:
    1. query.bindValue(":time",_time.toString("yyyyMMddhhmmss"));
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Oct 2009
    Location
    Mexico
    Posts
    81
    Thanks
    6
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Inserting blob into mysql using QT?

    i create a class to manage BLOB fields. i tested in SQLITE and FIREBIRD. i think must work in MYSQL

    Qt Code:
    1. class SqlFieldBlob : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit SqlFieldBlob(QObject *parent = 0);
    6.  
    7. QByteArray data() { return p_byteArray; }
    8. bool saveData(const QString &filename);
    9.  
    10. public slots:
    11. void setData(const QByteArray &byteArray) { p_byteArray = byteArray;}
    12. void setFileName(const QString &filename);
    13. void setPixmap(const QPixmap &pixmap);
    14.  
    15. private:
    16. QByteArray p_byteArray;
    17. };
    To copy to clipboard, switch view to plain text mode 

    and using something like
    Qt Code:
    1. SqlFieldBlob blob;
    2. blob.setFileName(fileName);
    3.  
    4. QSqlQuery query2;
    5. query2.prepare("INSERT INTO files (file_name, file_data) "
    6. "VALUES (:id, :id2)");
    7. query2.bindValue("id", fileName);
    8. query2.bindValue("id2", blob.data());
    9. query2.exec();
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files
    Last edited by ecanela; 31st August 2010 at 04:31. Reason: updated contents

  5. The following user says thank you to ecanela for this useful post:

    guitronic (17th July 2014)

  6. #5
    Join Date
    Oct 2009
    Location
    Mexico
    Posts
    81
    Thanks
    6
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Inserting blob into mysql using QT?

    code to convert a QPixmap in a QByteArray

    Qt Code:
    1. QByteArray BlobClass::convertPixmap(const QPixmap &pixmap)
    2. {
    3. QByteArray bytes;
    4. QBuffer buffer(&bytes);
    5. buffer.open(QIODevice::WriteOnly);
    6. pixmap.save(&buffer, "PNG");
    7. return buffer.data();
    8. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. problem: inserting a BLOB into sqlite3 with QT4.3
    By oscar in forum Qt Programming
    Replies: 1
    Last Post: 18th November 2007, 12:38
  2. mysql configuration with qt
    By bala in forum Installation and Deployment
    Replies: 3
    Last Post: 6th November 2007, 11:02
  3. MySQL starting problem
    By shamik in forum General Discussion
    Replies: 5
    Last Post: 25th April 2007, 07:20
  4. Qt 4.1.4 & Mysql 5 on Linux x64
    By bothapn in forum Installation and Deployment
    Replies: 7
    Last Post: 4th August 2006, 13:23
  5. Qt 4.1 OS on Windows & mysql
    By neeko in forum Installation and Deployment
    Replies: 10
    Last Post: 31st January 2006, 20:22

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.