Skip to content

Improve binary value handling in queries (Python 3) #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

vtermanis
Copy link

To write raw binary values to e.g. a BINARY or BLOB column, one has to currently e.g. produce a hexadecimal representation to make sure MySQL always (and not just when the binary data is e.g. ASCII) interprets the input correctly:

raw_data = b'somebinarygoeshere'
cursor.exectute('INSERT INTO somewhere VALUES (UNHEX(%s))', (binascii.b2a_hex(raw_data),)

With the proposed change (only applicable to Python 3):

cursor.exectute('INSERT INTO somewhere VALUES (%s)', (raw_data,))

This is done by prepending the _binary prefix, see e.g. here and here.

For Python 2, bytearray could be handled in a similar fashion but I wasn't sure how to best implement this in the C extension given it only deals with bytes and not bytearray currently, unlike the pure-Python version (where one could simply always prepend _binary if buf is bytearray (in conversion.py).

@mysql-oca-bot
Copy link

Hi, thank you for your contribution. Please confirm this code is submitted under the terms of the OCA (Oracle's Contribution Agreement) you have previously signed by cutting and pasting the following text as a comment:
"I confirm the code being submitted is offered under the terms of the OCA, and that I am authorized to contribute it."
Thanks

@vtermanis
Copy link
Author

I confirm the code being submitted is offered under the terms of the OCA, and that I am authorized to contribute it.

@mysql-oca-bot
Copy link

Hi, thank you for your contribution. Your code has been assigned to an internal queue. Please follow
bug http://bugs.mysql.com/bug.php?id=82517 for updates.
Thanks

@vtermanis vtermanis deleted the binary_values branch August 9, 2016 16:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants