PyQt4: Reading binary data from Postgresql fails under Windows
-
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 fieldfile=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')
@ -
Hi and welcome to devnet,
It rather looks like your software doesn't find the PostgreSQL client libraries. Check with something like Dependency Walker
-
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
-
A text file works great
-
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.
-
It's not OS nor Qt specific. A file extension doesn't do anything magic when writing programmatically to a file