Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSqlQuery can not add QByteArray to BLOB column without CLIENT_COMPRESS option
Forum Updated to NodeBB v4.3 + New Features

QSqlQuery can not add QByteArray to BLOB column without CLIENT_COMPRESS option

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 783 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    QLab
    wrote on 2 Jul 2018, 12:55 last edited by
    #1

    Hi,

    I tried to insert a file into BLOB column in MariaDB like

    QSqlQuery query(db);
    query.prepare(
                    R"(INSERT INTO Thesis (UserId, Title, File)
                       VALUES (:userId, :title, :file)
                       ON DUPLICATE KEY UPDATE Title = :title)");
    query.bindValue(":userId", 1);
    query.bindValue(":title", "example");
    
    QByteArray ba;
    QFile f("/home/document.pdf");
    if(f.open(QIODevice::ReadOnly)) {
        ba = f.readAll();
        f.close();
    }
    
    query.bindValue(":file", ba, QSql::In | QSql::Binary);
    
    if (!query.exec()) {
        qWarning()<< Q_FUNC_INFO << query.lastError();
    }
    

    above code always generate errors

    QSqlError("2006", "QMYSQL3: Unable to execute statement", "MySQL server has gone away")
    

    but when I set CLIENT_COMPRESS connect option

    db.setConnectOptions("CLIENT_COMPRESS=1");
    

    then the above code works fine. I found that sometimes I also need to set additional options like

    db.setConnectOptions("MYSQL_OPT_RECONNECT=1;MYSQL_OPT_WRITE_TIMEOUT=1000;CLIENT_COMPRESS=1");
    

    to make it work, someone know what causes that issue?

    I increased max_allowed_packet option on the server but that was not necessary since file is < 2MB

    MariaDB [testdb]> SHOW VARIABLES LIKE '%packet%';
    +--------------------------+------------+
    | Variable_name            | Value      |
    +--------------------------+------------+
    | max_allowed_packet       | 268435456  |
    | slave_max_allowed_packet | 1073741824 |
    +--------------------------+------------+
    

    Software:
    10.1.26-MariaDB server
    Qt 5.10.1
    Debian 9

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 2 Jul 2018, 21:13 last edited by
      #2

      Hi,

      Do you have the same problem if you use MySQL ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      1/2

      2 Jul 2018, 12:55

      • Login

      • Login or register to search.
      1 out of 2
      • First post
        1/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved