#10070: Named parameters not working on raw sql queries with sqlite
-------------------------------------+-------------------------------------
     Reporter:  Matias Surdi         |                    Owner:  Ryan
                                     |  Cheley
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  dev
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  1                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Ryan Cheley):

 I've done a bit of research and it looks like
 `django.db.backends.sqlite3.base` has a class called
 `SQLiteCursorWrapper`.

 This in turn has a method `convert_query` which per the doc string of the
 `SQLiteCursorWrapper ` method does:

     Django uses "format" style placeholders, but pysqlite2 uses "qmark"
 style.
     This fixes it -- but note that if you want to use a literal "%s" in a
 query,
     you'll need to use "%%s".

 I think the way to fix the issue is to update that method to include the
 following logic:


 {{{
 query  = re.sub("\)s", "",  re.sub("%\(", ":", query))
 }}}

 this will convert the string passed using the expectations of the ORM API
 to what SQLite is expecting for parameters, i.e. a passed in query will go
 from this

 {{{
 'select * from content_injuredlist where id =  %(id)s and app_name =
 %(app_name)s'
 }}}

 to this

 {{{
 'select * from content_injuredlist where id =  :id and app_name =
 :app_name'
 }}}

 which will then work with SQLite

 Still to do:

 - Update the method
 - Write any tests
 - submit PR

-- 
Ticket URL: <https://code.djangoproject.com/ticket/10070#comment:27>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018429bdc3e4-259a5e81-bc35-4eb6-bc9e-87eb607e2751-000000%40eu-central-1.amazonses.com.

Reply via email to