Results 1 to 2 of 2

Thread: QSqlDatabase and Postgres - Insert bytes into database

  1. #1
    Join Date
    Sep 2010
    Posts
    4
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Exclamation QSqlDatabase and Postgres - Insert bytes into database

    I have a Postgres database with a BYTEA field.
    I want to insert bytes into this fields.

    I'm currently doing this:

    Qt Code:
    1. QByteArray data = <get data>;
    2. QString query = "INSERT INTO images (id, bytes) VALUES (1, :data)";
    3. QSqlQuery sql(query);
    4. sql.bindValue(":data", data);
    5. sql.exec();
    To copy to clipboard, switch view to plain text mode 

    This failes and I get the following error:
    Qt Code:
    1. ERROR: syntax error at or near "("
    2. LINE 1: EXECUTE ('\\000\\000\\000\\000\\000\\000\\000\\000\\000\\00...
    To copy to clipboard, switch view to plain text mode 

    If I replace the :data with NULL, everything works fine.

    Does anyone know how to insert binary dat into Postgres using QSqlDatabase and QSqlQuery.
    Any reference to an online tutorial will also be greate, because I can't find one.

    Thank you

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QSqlDatabase and Postgres - Insert bytes into database

    When I remember right it's a "problem" of the driver. You have to convert it to base64 before inserting:
    Qt Code:
    1. sql.bindValue(":data", data.toBase64());
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Releasing database file with QSqlDatabase
    By JPNaude in forum Qt Programming
    Replies: 24
    Last Post: 24th May 2011, 10:33
  2. How to insert row to SQLite database?
    By MIH1406 in forum Qt Programming
    Replies: 6
    Last Post: 29th May 2010, 13:22
  3. QSqlDatabase - How to connect to multiple database?
    By cutie.monkey in forum Qt Programming
    Replies: 4
    Last Post: 10th March 2010, 13:03
  4. How to BackUp DataBase and Restore it Use QSqlDataBase?
    By litterflybug in forum Qt Programming
    Replies: 1
    Last Post: 6th December 2009, 05:34
  5. how to insert an ' in a database
    By jh in forum General Programming
    Replies: 3
    Last Post: 17th August 2006, 03:47

Tags for this Thread

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.