#312 Unicode2Str converter unusable

MySQLdb-1.2
closed
Andy Dustman
MySQLdb (285)
5
2012-09-19
2011-05-25
Anonymous
No

The Unicode2Str function in MySQLdb/converters.py isn't usable as is since it fails to escape and quote the encoded string. This results in the bare text being interpolated into the SQL. If the text is a single word it is likely to result in an exception such as

OperationalError: (1054, Unknown column 'param_value' in 'where clause')

Ignoring questions about the correct encoding to use and how this should interact with the charset setting it seems to me the default Unicode2Str should do one of two things:

1) raise a NotImplementedError('you must provide a unicode conversion function'), or

2) do the intuitive thing:

def Unicode2Str(s, d):
return string_literal(s.encode('utf8'), d)

Discussion

  • Andy Dustman
    Andy Dustman
    2011-05-25

    You're doing it wrong. There's no good reason to use Unicode2Str in user code. Otherwise, attach an example demonstrating the problem.