0

my default mysql paramstle is :'format'

>>>MySQLdb.paramstyle
'format'

i want to change default paramstyle to 'pyformat' ,because of i can have query something like "WHERE name=%(name)s". is there any way to do that?

Mahyar
  • 796
  • 4
  • 16
  • 34

1 Answers1

1

I'm pretty sure this isn't possible without monkeying around inside the librarie's internals... -- The paramstyle is hardcoded to conform to PEP 249.

Also notice that the escaping always uses a tuple ...

I imagine you could get that to work if you replaced:

if args is not None:
    query = query % tuple(( get_codec(a, self.encoders)(db, a) for a in args ))

with something like:

if args is not None:
    if isinstance(args, tuple):
        args = tuple(get_codec(a, self.encoders)(db, a) for a in args)
    elif isinstance(args, dict):
        args = {k: get_codec(a, self.encoders)(db, a) for a in args}
    query = query % args

Here you'd probably want to do this in a Cursor subclass -- and you can pass that as an argument to the connection...

Ultimately, this is a bit "hairy" since re-writing the entire execute method will likely force you to use a bunch of stuff that isn't in the "public" API, but it should be possible at least.

mgilson
  • 300,191
  • 65
  • 633
  • 696
  • 1
    @Mahyar -- Sure there's a way. The question is how _easy_ is it to do and how much you're willing to rely on implementation details to do it :-) – mgilson Aug 11 '14 at 06:29
  • 1
    @Mahyar -- Interestingly enough, if you read the _docstring_, it acts like the `%(...)` format should be supported, but looking at the code, I have a hard time believing that it is. :-) – mgilson Aug 11 '14 at 06:33