Results 1 to 3 of 3

Thread: Insert image (PNG) into MySQL table (longblob)

  1. #1
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Insert image (PNG) into MySQL table (longblob)

    Hi,
    I'm calling primeInsert(int,QSqlRecord&) in order to set initial values for a QTableView/QSqlTableModel approach (not sure about this).
    Qt Code:
    1. void Dialog::primeInsertImage(int row, QSqlRecord &record)
    2. {
    3. record.setValue("id", generateNextId("object")); // To get the next free id
    4. record.setValue("name", ui->fileNameLabel->text()); // To get a QString from a QLabel
    5. QImage currentImage = ui->imageLabel->pixmap()->toImage();
    6. QByteArray bytes;
    7. QBuffer buffer(&bytes);
    8. buffer.open(QIODevice::WriteOnly);
    9. currentImage.save(&buffer, "PNG");
    10. record.setValue("image", bytes); //To get the image previously loaded into a QLabel
    11. }
    To copy to clipboard, switch view to plain text mode 

    The insert code is:
    Qt Code:
    1. void Dialog::on_InsertRecord_clicked()
    2. {
    3. view->setFocus();
    4. int row = model->rowCount();
    5. model->insertRows(row,1);
    6. QModelIndex index = model->index(row, model->fieldIndex("id"));
    7. view->setCurrentIndex(index);
    8. view->edit(index);
    9. }
    To copy to clipboard, switch view to plain text mode 

    It works fine until i press enter (change record in the view).

    Then i only get one fiel with data (The id).
    The "name" and "image" fiels are NULL!

    What could i be doing wrong?

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    507
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Insert image (PNG) into MySQL table (longblob)

    Hi, just a guess (I have never used QSqlRecord):
    the docs say "When queries are generated to be executed on the database only those fields for which isGenerated() is true are included in the generated SQL."
    So maybe your data is not sent to your database?

    Ginsengelf

  3. The following user says thank you to Ginsengelf for this useful post:

    graciano (2nd April 2014)

  4. #3
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Insert image (PNG) into MySQL table (longblob)

    Good point Ginsengelf!

    I checked the value of isGenerated() and it was "false" for all fields.

    I just added:
    Qt Code:
    1. record.setGenerated("id", true);
    2. record.setGenerated("name", true);
    3. record.setGenerated("image", true);
    To copy to clipboard, switch view to plain text mode 

    Now it sends all data to the database.
    I still don't get why the primary key ("id") was sent anyway!

    Thanks

Similar Threads

  1. Replies: 9
    Last Post: 14th February 2013, 12:14
  2. Replies: 2
    Last Post: 17th February 2010, 14:32
  3. unable to insert into mysql table
    By mohanakrishnan in forum Newbie
    Replies: 4
    Last Post: 5th December 2009, 05:02
  4. unable to insert into table
    By mohanakrishnan in forum Qt Programming
    Replies: 0
    Last Post: 4th December 2009, 10:52
  5. MYSQL 5 Table qt model as small Mysql admin
    By patrik08 in forum Qt-based Software
    Replies: 0
    Last Post: 1st May 2007, 09:43

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.