Search code examples
pythonsqlutf-8nuodb

invalid UTF-8 code sequence


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


Solution

  • 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.