I'm trying to run a sql query with parameters taken from a dict, here is
the relevant part of the code:
query = "select * from table where name='%(name)s'"
parameters = {'name':'valueofname'}
cursor = connection.cursor()
data = cursor.execute(query,parameters)
The error I get is:
TypeError: format requires a mapping
But, as far as I know (from PEP249) this should be possible.
Basically, what I need, is to pass the parameters values in a dict, and
not as a list or tuple as is shown in django docs.
Which is the correct way to accomplish this?
Thanks a lot.
Hi,
I'm trying to run a sql query with parameters taken from a dict, here is
the relevant part of the code:
query = "select * from table where name='%(name)s'"
From what I have seen by reading DB backend source code, Django cursor's
execute() method supports only the printf-like parmeter maker style with a list
or tuple of actual parameters.
If you want to use the pyformat parameter marking style (as described
in PEP 249),
you' ll need to use the native DB-API driver API as you've already discovered.
Regards,
--
Ramiro Morales
>
> That's using MySQL as the DB, so perhaps it is backend-specific?
So it seems. the MySQL and PostgreSQL backend seem to (still?)
support it. But the Oracle crew removed it a while back:
http://code.djangoproject.com/changeset/9418
Regards,
--
Ramiro Morales
Yes, maybe it's just a problem with sqlite, which is te backend I'm
using.I'll try with mysql later.
Is this a bug? should it be reported as a ticket?
Just to be accurate, PEP 249 says that a paramstyle of "pyformat" is one
possible value, which would permit the above sort of query. It does not
say that every conforming database wrapper is required to support it.
Although the PEP recommends supporting "numeric", "named" or pyformat"
styles of parameter markers, historically and practically, most database
wrappers have supported "format", although SQLite (and some other
databases that don't have backends in Django's core, although the exact
names escape me now) only supports "qmark".
Since you note later in the thread that you're using SQLite, that is the
cause of the confusion here. I don't think there's any bug.
Regards,
Malcolm
If you want. Personally, I'm struggling to care about it that much. It's
simply not a blocker to any real functionality.
Malcolm