<div dir="ltr"><div><div><div><div><div><div><div><div>I'm `PyMySQL <<a href="https://github.com/pymysql/pymysql">https://github.com/pymysql/pymysql</a>>`_ (pure Python MySQL driver) developer.<br></div><div>I share my experience that I've suffered by bytes doesn't have %-format.<br>
</div><div><br></div>`MySQL-python <<a href="https://github.com/farcepest/MySQLdb1">https://github.com/farcepest/MySQLdb1</a>>`_ is a most major DB-API 2.0 driver for MySQL.<br></div>Other MySQL drivers like PyMySQL, MySQL-connector-python are designed compatible it as possible.<br>
</div>MySQL-python uses 'format' paramstyle.<br><br><a href="http://www.python.org/dev/peps/pep-0249/#paramstyle">http://www.python.org/dev/peps/pep-0249/#paramstyle</a><br><a href="https://github.com/farcepest/MySQLdb1/blob/master/MySQLdb/__init__.py#L27">https://github.com/farcepest/MySQLdb1/blob/master/MySQLdb/__init__.py#L27</a><br>
<br></div>MySQL protocol is basically encoded text, but it may contain arbitrary (escaped) binary.<br></div>Here is simplified example constructing real SQL from SQL format and arguments. (Works only on Python 2.7)<br><br>
</div><span style="font-family:courier new,monospace">def escape_string(s):<br> return s.replace("'", "''")<br><br>def convert(x):<br> if isinstance(x, unicode):<br> x = x.encode('utf-8') # Use encoding assigned to connection in real.<br>
if isinstance(x, str):<br> x = "'" + escape_string(x) + "'" # 'quoted and '' escaped string'<br> else:<br> x = str(x) # like 42<br> return x<br><br>def build_query(query, *args):<br>
if isinstance(query, unicode):<br> query = query.encode('utf-8')<br> return query % tuple(map(convert, args))<br><br>textdata = b"hello"<br>bindata = b"abc\xff\x00"<br>query = "UPDATE table SET textcol=%s bincol=%s"<br>
<br>print build_query(query, textdata, bindata)</span><br><br><br></div><div>I can't port this to Python 3.<br></div><div>Fortunately, MySQL supports hex string like x'616263ff00'<br></div><div>So I use it and PyMySQL supports binary data on Python 3.<br>
But hex string consumes double space than normal (escaped) bytes.<br></div><div>This is why I don't use hexstring on Python 2.<br></div><div><br><a href="https://github.com/PyMySQL/PyMySQL/blob/master/pymysql/converters.py#L303">https://github.com/PyMySQL/PyMySQL/blob/master/pymysql/converters.py#L303</a><br>
<a href="https://github.com/PyMySQL/PyMySQL/blob/master/pymysql/converters.py#L71">https://github.com/PyMySQL/PyMySQL/blob/master/pymysql/converters.py#L71</a><br></div><div><br></div></div><div><div><div><div></div></div>
</div></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Wed, Jan 8, 2014 at 8:20 AM, Mark Janssen <span dir="ltr"><<a href="mailto:dreamingforward@gmail.com" target="_blank">dreamingforward@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">>>> The trouble (for me) comes in when I try to use single bytes,<br>
>>> either when creating or extracting.<br>
>><br>
>> Hmm... aren't you exagerating the trouble? It's not very difficult to<br>
>> work with single bytes in Python 3...<br>
><br>
> No, I'm not. I don't think of b'C' as the integer 67 any more than I think<br>
> of the number 256 as the bytes b'\x01\xFF'.<br>
<br>
</div>There's something fundamentally wrong with these brainfarts coming out<br>
on the list. Just how, Ethan, did you think you could represent<br>
binary data in a text string, whether preceded by the char 'b' or not?<br>
What did you think you would do when you got to character 0, the<br>
first (pseudo)-symbol in ASCII?<br>
<br>
Why don't you jackasses start listening instead of wanking each other<br>
with bullshit?<br>
<br>
markj<br>
<div class="HOEnZb"><div class="h5">_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br>INADA Naoki <<a href="mailto:songofacandy@gmail.com">songofacandy@gmail.com</a>>
</div>