3

I'm trying to do a simple connect using a NuoDB database -

import pynuodb
connection = pynuodb.connect("DB", "servername", "adminaccount", "password", options={'schema': 'schemaname'})
cursor = connection.cursor()
thedata = open('file.pdf', 'rb').read()
sql = "update table set column = (?) where id = 1"
cursor.execute(sql, (thedata,))

in trying to load it's generating the following error -

INVALID_UTF8:  invalid UTF-8 code sequence

UPDATE - I've tried using both a BLOB or BINARY and both generate the same error. The documentation on the data types is found here -

http://doc.nuodb.com/display/doc/Binary+Data+Types

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
whoisearth
  • 4,080
  • 13
  • 62
  • 130
  • 2
    Is the column a `text` or `varchar` column? You can't store binary data (such as a PDF) in a text column. – Thanatos Jul 07 '14 at 18:30
  • @Thanatos - it's a BLOB column. Reference the spec here - http://doc.nuodb.com/display/doc/Binary+Data+Types – whoisearth Jul 07 '14 at 18:32

1 Answers1

1

The pynuodb library gives you a special type to encapsulate binary data, pynuodb.Binary(); wrap binary data in that object to ensure correct handling:

thedata = pynuodb.Binary(open('file.pdf', 'rb').read())
sql = "update table set column = (?) where id = 1"
cursor.execute(sql, (thedata,))

Without this wrapper, the driver tries to send the data to the server as text, which must be UTF-8 encoded.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • That worked and gave no errors! Would the export be the same just so I can confirm the import worked correctly. – whoisearth Jul 07 '14 at 19:32
  • @whoisearth: you only need to use the wrapper when you want to use binary data as a query parameter. `select column from table where id = 1'` should just work. – Martijn Pieters Jul 07 '14 at 19:45