I'm `PyMySQL <https://github.com/pymysql/pymysql>`_ (pure Python MySQL driver) developer.
I share my experience that I've suffered by bytes doesn't have %-format.

`MySQL-python <https://github.com/farcepest/MySQLdb1>`_ is a most major DB-API 2.0 driver for MySQL.
Other MySQL drivers like PyMySQL, MySQL-connector-python are designed compatible it as possible.
MySQL-python uses 'format' paramstyle.

http://www.python.org/dev/peps/pep-0249/#paramstyle
https://github.com/farcepest/MySQLdb1/blob/master/MySQLdb/__init__.py#L27

MySQL protocol is basically encoded text, but it may contain arbitrary (escaped) binary.
Here is simplified example constructing real SQL from SQL format and arguments. (Works only on Python 2.7)

def escape_string(s):
    return s.replace("'", "''")

def convert(x):
    if isinstance(x, unicode):
        x = x.encode('utf-8')  # Use encoding assigned to connection in real.
    if isinstance(x, str):
        x = "'" + escape_string(x) + "'"  # 'quoted and '' escaped string'
    else:
        x = str(x)  # like 42
    return x

def build_query(query, *args):
    if isinstance(query, unicode):
        query = query.encode('utf-8')
    return query % tuple(map(convert, args))

textdata = b"hello"
bindata = b"abc\xff\x00"
query = "UPDATE table SET textcol=%s bincol=%s"

print build_query(query, textdata, bindata)



I can't port this to Python 3.
Fortunately, MySQL supports hex string like x'616263ff00'
So I use it and PyMySQL supports binary data on Python 3.
But hex string consumes double space than normal (escaped) bytes.
This is why I don't use hexstring on Python 2.

https://github.com/PyMySQL/PyMySQL/blob/master/pymysql/converters.py#L303
https://github.com/PyMySQL/PyMySQL/blob/master/pymysql/converters.py#L71



On Wed, Jan 8, 2014 at 8:20 AM, Mark Janssen <dreamingforward@gmail.com> wrote:
>>> The trouble (for me) comes in when I try to use single bytes,
>>> either when creating or extracting.
>>
>> Hmm... aren't you exagerating the trouble? It's not very difficult to
>> work with single bytes in Python 3...
>
> No, I'm not.  I don't think of b'C' as the integer 67 any more than I think
> of the number 256 as the bytes b'\x01\xFF'.

There's something fundamentally wrong with these brainfarts coming out
on the list.  Just how, Ethan, did you think you could represent
binary data in a text string, whether preceded by the char 'b' or not?
 What did you think you would do when you got to character 0, the
first (pseudo)-symbol in ASCII?

Why don't you jackasses start listening instead of wanking each other
with bullshit?

markj
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/



--
INADA Naoki  <songofacandy@gmail.com>