Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. PyQt4: Reading binary data from Postgresql fails under Windows
Forum Update on Monday, May 27th 2025

PyQt4: Reading binary data from Postgresql fails under Windows

Scheduled Pinned Locked Moved Language Bindings
11 Posts 2 Posters 3.3k Views
  • 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.
  • L Offline
    L Offline
    leonidas
    wrote on 27 Feb 2015, 13:46 last edited by
    #1

    I need to read from Postgresql bytea field that stores binary data and save to disk as pdf.
    I use the following code, which it works under linux but fails under Windows.
    @ self.db = QtSql.QSqlDatabase.database("my_connection")
    if self.db.open():
    query = QtSql.QSqlQuery(self.db)
    query.exec_("SELECT Data FROM files where id =1")
    query.next() # get first row
    bytearr = query.value(0) # get the value of first field

            file=QTemporaryFile(QDir.tempPath() + "/XXXXXX." + '.pdf')
    
            file.setAutoRemove(False)
            file.open(QIODevice.WriteOnly)
            file.writeData(bytearr)
    
            QDesktopServices.openUrl(QUrl("file:///" + file.fileName()))# open pdf
            file.close()
    

    @

    The saved file looks corrupted when I run the code under Windows. There is no problem on Linux.
    Could it be a PyQt version problem???

    On Arch linux
    @
    ('Qt version:', '4.8.6')
    ('SIP version:', '4.16.4')
    ('PyQt version:', '4.10.4-snapshot-f62fabcefe39')
    @

    On WINDOS 7
    @('Qt version:', '4.7.1')
    ('SIP version:', '4.13.1')
    ('PyQt version:', '4.8.3')
    @

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 27 Feb 2015, 22:41 last edited by
      #2

      Hi and welcome to devnet,

      It rather looks like your software doesn't find the PostgreSQL client libraries. Check with something like Dependency Walker

      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
      • L Offline
        L Offline
        leonidas
        wrote on 28 Feb 2015, 05:58 last edited by
        #3

        I can connect to Postgresql and execute other queries but that one has a problem.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 28 Feb 2015, 23:00 last edited by
          #4

          Sorry, I've misunderstood your original problem. Saving a byte array in a file named something.pdf doesn't make it a valid pdf file. If you want to create a PDF you should create one using e.g. QPdfWriter

          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
          • L Offline
            L Offline
            leonidas
            wrote on 1 Mar 2015, 08:20 last edited by
            #5

            Ok, the file can be any file format in the database i.e. zip, pdf,doc,jpg etc. Is there any generic way to exctract from database and write to disk?

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 2 Mar 2015, 00:00 last edited by
              #6

              A text file works great

              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
              • L Offline
                L Offline
                leonidas
                wrote on 2 Mar 2015, 05:59 last edited by
                #7

                Thank you. I 'll check it. I have already checked a pdf and an xml

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 3 Mar 2015, 00:42 last edited by
                  #8

                  QFile doesn't know how to write pdf or xml just because you put that extension on the file name. It's your job to provide the right data.

                  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
                  • L Offline
                    L Offline
                    leonidas
                    wrote on 3 Mar 2015, 05:35 last edited by
                    #9

                    Is that an OS specific issue? It works under Linux and not under Windows?

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 3 Mar 2015, 23:50 last edited by
                      #10

                      It's not OS nor Qt specific. A file extension doesn't do anything magic when writing programmatically to a file

                      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
                      • L Offline
                        L Offline
                        leonidas
                        wrote on 4 Mar 2015, 05:48 last edited by
                        #11

                        How is it explained that the code works under Linux?
                        With the same code I can write pdf and zip.
                        Thanks for your patience!

                        1 Reply Last reply
                        0

                        1/11

                        27 Feb 2015, 13:46

                        • Login

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