Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Dec. 3, 2010 16:37:20

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

query date by string


in php I can run the following query for mysql

SELECT * FROM some_table WHERE some_date LIKE "2010%"

How do i do this in django using cursor.execute()?

--
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. 3, 2010 16:45:04

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

query date by string


You should be able to do it just as you mention, passing the query
string to cursor.execute().

Seehttp://docs.djangoproject.com/en/dev/topics/db/sql/#executing-custom-sql-directlyHave you tried this? Are you getting an error? If so, what is it?

--
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. 3, 2010 16:47:35

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

query date by string


It says incorrect date value.

On Dec 3, 8:44 am, wayne <wayne.tuxro...@gmail.com> wrote:
> You should be able to do it just as you mention, passing the query
> string to cursor.execute().
>
> Seehttp://docs.djangoproject.com/en/dev/topics/db/sql/#executing-custom-...
>
> Have you tried this?  Are you getting an error?  If so, what is it?

--
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. 3, 2010 17:04:13

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

query date by string


On Dec 3, 10:47 am, owidjaya <owidj...@gmail.com> wrote:
> It says incorrect date value.
>

What database are you using? I'm wondering if this is an issue of
formatting the sql correctly. Did you read the snippet near the
bottom of the page I linked to? Try:

SELECT * FROM some_table WHERE some_date LIKE "%s" % '2010%'

--
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

#5 Dec. 3, 2010 17:13:38

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

query date by string


It's for mysql
it still does not work. I tried it with the following sql statement
"SELECT * FROM project_project WHERE start_date LIKE '%s'" % '2010%%'


On Fri, Dec 3, 2010 at 9:04 AM, wayne <wayne.tuxro...@gmail.com> wrote:

>
>
> On Dec 3, 10:47 am, owidjaya <owidj...@gmail.com> wrote:
> > It says incorrect date value.
> >
>
> What database are you using? I'm wondering if this is an issue of
> formatting the sql correctly. Did you read the snippet near the
> bottom of the page I linked to? Try:
>
> SELECT * FROM some_table WHERE some_date LIKE "%s" % '2010%'
>
> --
> 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<django-users%2bunsubscr...@googlegroups.com>
> .
> For more options, visit this group at
>http://groups.google.com/group/django-users?hl=en.
>
>

--
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

#6 Dec. 3, 2010 17:14:13

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

query date by string


It's for mysql
it still does not work. I tried it with the following sql statement
"SELECT * FROM project_project WHERE start_date LIKE '%s'" % '2010%%'

On Dec 3, 9:04 am, wayne <wayne.tuxro...@gmail.com> wrote:
> On Dec 3, 10:47 am, owidjaya <owidj...@gmail.com> wrote:
>
> > It says incorrect date value.
>
> What database are you using?  I'm wondering if this is an issue of
> formatting the sql correctly.  Did you read the snippet near the
> bottom of the page I linked to?  Try:
>
> SELECT * FROM some_table WHERE some_date LIKE "%s" % '2010%'

--
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

#7 Dec. 3, 2010 17:28:50

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

query date by string


On Dec 3, 11:13 am, oscar widjaya <owidj...@gmail.com> wrote:
> It's for mysql
> it still does not work. I tried it with the following sql statement
> "SELECT * FROM project_project WHERE start_date LIKE '%s'" % '2010%%'

Well, I assume that the double % at the end of your query is
unintentional, as I don't think it should be there, but I'm not an sql
guru.

I just know that when I was having to craft some custom sql a couple
of months ago (using Postgres) I had to toy with the formatting a bit
to get it working. I also had to make sure I passed everything as a
str, and not unicode.

Maybe someone else how knows sql better will come along.

--
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

#8 Dec. 3, 2010 17:30:16

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

query date by string


On 3 déc, 17:37, owidjaya <owidj...@gmail.com> wrote:
> in php I can run the following query for mysql
>
> SELECT * FROM some_table WHERE some_date LIKE "2010%"
>
> How do i do this in django using cursor.execute()?

sql = "SELECT * FROM some_table WHERE YEAR(some_date)=%s"
cursor.execute(sql, (2010,))

--
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

#9 Dec. 3, 2010 17:34:41

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

query date by string


so you are saying that SELECT * FROM some_table WHERE some_date LIKE
"2010%"
is not possible?

On Dec 3, 9:30 am, bruno desthuilliers <bruno.desthuilli...@gmail.com>
wrote:
> On 3 déc, 17:37, owidjaya <owidj...@gmail.com> wrote:
>
> > in php I can run the following query for mysql
>
> > SELECT * FROM some_table WHERE some_date LIKE "2010%"
>
> > How do i do this in django using cursor.execute()?
>
> sql = "SELECT * FROM some_table WHERE YEAR(some_date)=%s"
> cursor.execute(sql, (2010,))

--
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 23rd of October
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