Skip to content
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

Add _binary prefix for binary data #323

Closed
LemonPi opened this issue Jul 25, 2018 · 5 comments · Fixed by #326
Closed

Add _binary prefix for binary data #323

LemonPi opened this issue Jul 25, 2018 · 5 comments · Fixed by #326
Assignees
Labels

Comments

@LemonPi
Copy link

LemonPi commented Jul 25, 2018

This was a problem introduced in MySQL 5.6.27 and pointed out PyMySQL/mysqlclient#81
and PyMySQL/mysqlclient#106

Without it inserting bytes to a VARBINARY() or BLOB column can produce wrong warnings such as

Warning: Invalid utf8mb4 character string: 'B017'

@terrycain terrycain self-assigned this Jul 25, 2018
@terrycain terrycain added the bug label Jul 25, 2018
@terrycain
Copy link
Collaborator

Dont suppose you can provide a sample test case?

@LemonPi
Copy link
Author

LemonPi commented Jul 26, 2018

Sure I'll create it tomorrow afternoon

@LemonPi
Copy link
Author

LemonPi commented Jul 26, 2018

import asyncio
import aiomysql


async def test_binary_insert():
    # adjust user and password to be your local user or root
    async with aiomysql.connect(host='127.0.0.1', user='', password='') as connection:
        async with connection.cursor() as cursor:
            create_db = "CREATE DATABASE IF NOT EXISTS bugtest"
            await cursor.execute(create_db)

            create_table = """CREATE TABLE IF NOT EXISTS `bugtest`.`testtable` (
              `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
              `bindata` VARBINARY(200) NOT NULL,
              PRIMARY KEY (`id`)
            );"""

            await cursor.execute(create_table)

        async with connection.cursor() as cursor:
            # Warning: Invalid utf8mb4 character string: 'B017'
            insert_row = "INSERT INTO `bugtest`.`testtable` (bindata) VALUES (%s)"
            # no warnings when run with below
            insert_row = "INSERT INTO `bugtest`.`testtable` (bindata) VALUES (_binary %s)"

            await cursor.execute(insert_row, (b'\xB0\x17'))

        async with connection.cursor() as cursor:
            get_row = "SELECT * FROM `bugtest`.`testtable`"

            await cursor.execute(get_row)
            rows = await cursor.fetchall()

            # will be inserted regardless of the warning
            for row in rows:
                print(row)

            # clean for retest
            await cursor.execute("DELETE FROM `bugtest`.`testtable`")


asyncio.get_event_loop().run_until_complete(test_binary_insert())

@LemonPi
Copy link
Author

LemonPi commented Jul 31, 2018

Hey @terrycain were you able to replicate the warnings with the code snippet?

@terrycain
Copy link
Collaborator

terrycain commented Jul 31, 2018

^^ :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants