Django: using named parameters in a raw SQL query

I am trying to execute a raw query that is being created dynamically. To make sure that the parameters are inserted into the actual position, I use named parameters.

This seems to work for Sqlite without a problem. (all my tests are successful) But when I run the same code against MariaDb, it fails.

A simple request example:

 SELECT u.* 
    FROM users_gigyauser AS u
  WHERE u.email like :u_email
    GROUP BY u.id
    ORDER BY u.last_login DESC
  LIMIT 60 OFFSET 0

Parameters:

 {'u_email': '%test%'}

The error I get is the default syntax error as the parameter is not replaced. I tried using "%" as an indicator, but this led to sql trying to parse

%u[_email]

and this led to a type error.

I execute a query like:

 raw_queryset = GigyaUser.objects.raw(self.sql_fetch, self._query_object['params'])

Or when counting:

 cursor.execute(self.sql_count, self._query_object['params'])

Both give the same error in MariaDB, but work on Sqlite (using the ':' indicator)

?

+4
1

:

s :

%(u_email)s
+4

All Articles