0

How do you insert a bytes object into a database. Whenever I attempt this I end up with an empty string (not NULL) being inserted.

with open(filepath, 'rb') as f:
    filehash = hashlib.md5(f.read()).hexdigest()
img_pkl = pickle.dumps(img, protocol=4)

record = self.tablemodel.record()
record.setValue('originfile_path', filepath)
record.setValue('originfile_hash', filehash)
record.setValue('image', img_pkl)
record.setValue('area', area)
self.tablemodel.insertRecord(-1, record)

The issue was noted on the mailing list but never addressed. https://www.riverbankcomputing.com/pipermail/pyqt/2016-April/037260.html It also turns out that it makes more sense to use the table model when you can instead of prepared statements (he also noted a bug in pyqt regarding exec() vs exec_().

Zak
  • 936
  • 10
  • 19

1 Answers1

0

You must explicitly convert to a QByteArray.

record.setValue('image', QtCore.QByteArray(img_pkl))

Note: you also have to convert numpy.float64 objects using float()

Zak
  • 936
  • 10
  • 19