Monday, January 23, 2012

Tedious issues escaping/quoting strings for MySQL? Use hex! It's awesome!

Hiya,
Ok sure yes you 'escape' all your strings for SQL (i.e. replace `'` with \' and so on for other nonprintable).
Sometimes this is unsuitable.

Did you know you can avoid all that palarva and just pass your strings as hex?

Check it out!

For a varchar (or blob, or whatev) column, just try instead of

insert col='blah' into mytable
=
insert col=0x626c6168 into mytable

How awesome is that? Who knew?

Also, btw, you can get mysqldump to also output blobs as hex, which makes for easier parsing - see the help output

No comments: