Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Dec. 8, 2010 17:21:06

d.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

IN clause in raw sql


Hi, I am trying to work with an in clause in my sql in a raw sql
statement. A simplified example:

<code>
>>>Category.objects.raw('select * from app_category where name
>>>in(\'Restaurants\',\'Fast Food\')')
<Category: Fast Food>
</code>

So that works fine with the in clause hard coded in there, and I could
simply build that sql string, but I am wondering if there is a way to
use the parameters argument of raw? I tried using a list with the
strings, and just a string itself with the escaped quotes, but
couldn't get either to work.

Thanks

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Dec. 8, 2010 17:40:27

Tom E.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

IN clause in raw sql


On Wed, Dec 8, 2010 at 5:21 PM, dgmyrs <dgm...@gmail.com> wrote:
> Hi, I am trying to work with an in clause in my sql in a raw sql
> statement.  A simplified example:
>
> <code>
>>>>Category.objects.raw('select * from app_category where name
>>>>in(\'Restaurants\',\'Fast Food\')')
> <Category: Fast Food>
> </code>
>
> So that works fine with the in clause hard coded in there, and I could
> simply build that sql string, but I am wondering if there is a way to
> use the parameters argument of raw?  I tried using a list with the
> strings, and just a string itself with the escaped quotes, but
> couldn't get either to work.
>
> Thanks
>

Why use raw? Why not just use the ORM?

Category.objects.filter(name__in=)

Cheers

Tom

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 Dec. 8, 2010 17:42:34

d.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

IN clause in raw sql


That's a simplified sql just as an example. The actual one I need the
in clause is a lot more complex and can't be handled in the ORM.


On Dec 8, 11:40 am, Tom Evans <tevans...@googlemail.com> wrote:
> On Wed, Dec 8, 2010 at 5:21 PM, dgmyrs <dgm...@gmail.com> wrote:
> > Hi, I am trying to work with an in clause in my sql in a raw sql
> > statement.  A simplified example:
>
> > <code>
> >>>>Category.objects.raw('select * from app_category where name
> >>>>in(\'Restaurants\',\'Fast Food\')')
> > <Category: Fast Food>
> > </code>
>
> > So that works fine with the in clause hard coded in there, and I could
> > simply build that sql string, but I am wondering if there is a way to
> > use the parameters argument of raw?  I tried using a list with the
> > strings, and just a string itself with the escaped quotes, but
> > couldn't get either to work.
>
> > Thanks
>
> Why use raw? Why not just use the ORM?
>
> Category.objects.filter(name__in=)
>
> Cheers
>
> Tom

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 Dec. 8, 2010 18:29:07

David D.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

IN clause in raw sql


On 08/12/10 17:42, dgmyrs wrote:
> That's a simplified sql just as an example. The actual one I need the
> in clause is a lot more complex and can't be handled in the ORM.
>



Anyway, see alsohttp://code.djangoproject.com/browser/django/trunk/django/db/models/sql/where.py#L177Build up a parameterised query string dynamically (eurgh, I know):

l = ('Restaurants','Fast Food',)

pquery = "SELECT id FROM app_category WHERE name IN (%s)" % ',
'.join(*len(l))

Category.objects.raw(pquery, l)




--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

Board footer

Moderator control

Enjoy the 16th of December
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support