Forums

Select statement not working in django

Hello

the select statement is not working..returns a empty list<br> <code> cur.execute("SElECT * FROM webapp_information WHERE dat BETWEEN date('now', 'start of day') AND date('now', 'localtime');") <br> o=cur.fetchall() <br> print(o) </code> i checked the database, the data are saved.. But i do not know what the error is thanks

Do you have any data that matches the criteria that you're specifying? Have you stored your dates in localtime or utc? Also, check that the 2 date functions that you're using are returning the dates that you think they are.

hello

I checked the data are stored as per my query. It saves in localtime date. modules.py-<br> from __future__ import unicode_literals from django.db import models class information(models.Model): login=models.CharField(max_length=100,) idd=models.CharField(max_length=100,) avatar_url=models.CharField(max_length=100,default='nll',null=True) gravatar_id=models.CharField(max_length=100,default='nll',null=True) url=models.CharField(max_length=100, default='nll',null=True) html_url=models.CharField(max_length=100,default='nll',null=True) followers_url=models.CharField(max_length=100,default='nll',null=True) following_url=models.CharField(max_length=100,default='nll',null=True) gists_url=models.CharField(max_length=100,default='nll',null=True) starred_url=models.CharField(max_length=100,default='nll',null=True) subscriptions_url=models.CharField(max_length=100,default='nll',null=True) organizations_url=models.CharField(max_length=100,default='nll',null=True) repos_url=models.CharField(max_length=100,default='nll',null=True) events_url=models.CharField(max_length=100,default='nll',null=True) received_events=models.CharField(max_length=100,default='nll',null=True) typ=models.CharField(max_length=100,default='nll',null=True) site_admin=models.CharField(max_length=100,default='nll',null=True) name=models.CharField(max_length=100,default='nll',null=True) company=models.CharField(max_length=100,default='nll',null=True) blog=models.CharField(max_length=100,default='nll',null=True) location=models.CharField(max_length=100,default='nll',null=True) email=models.CharField(max_length=100,default='nll',null=True) hireable=models.CharField(max_length=100,default='nll',null=True) bio=models.CharField(max_length=100,default='nll',null=True) public_repos=models.CharField(max_length=100,default='nll',null=True) public_gists=models.CharField(max_length=100,default='nll',null=True) followers=models.CharField(max_length=100,default='nll',null=True) following=models.CharField(max_length=100,default='nll',null=True) created_at=models.CharField(max_length=100,default='nll',null=True) updated_at=models.CharField(max_length=100,default='nll',null=True) dat = models.DateField() class sapi(models.Model): dat = models.DateField() It was worked two days ago.. But suddenly it is not working..thanks

hello

I checked the data are stored as per my query. It saves in localtime date. modules.py-<br> `from future import unicode_literals from django.db import models class information(models.Model): login=models.CharField(max_length=100,) idd=models.CharField(max_length=100,) avatar_url=models.CharField(max_length=100,default='nll',null=True) gravatar_id=models.CharField(max_length=100,default='nll',null=True) url=models.CharField(max_length=100, default='nll',null=True) html_url=models.CharField(max_length=100,default='nll',null=True) followers_url=models.CharField(max_length=100,default='nll',null=True) following_url=models.CharField(max_length=100,default='nll',null=True) gists_url=models.CharField(max_length=100,default='nll',null=True) starred_url=models.CharField(max_length=100,default='nll',null=True) subscriptions_url=models.CharField(max_length=100,default='nll',null=True) organizations_url=models.CharField(max_length=100,default='nll',null=True) repos_url=models.CharField(max_length=100,default='nll',null=True) events_url=models.CharField(max_length=100,default='nll',null=True) received_events=models.CharField(max_length=100,default='nll',null=True) typ=models.CharField(max_length=100,default='nll',null=True) site_admin=models.CharField(max_length=100,default='nll',null=True) name=models.CharField(max_length=100,default='nll',null=True) company=models.CharField(max_length=100,default='nll',null=True) blog=models.CharField(max_length=100,default='nll',null=True) location=models.CharField(max_length=100,default='nll',null=True) email=models.CharField(max_length=100,default='nll',null=True) hireable=models.CharField(max_length=100,default='nll',null=True) bio=models.CharField(max_length=100,default='nll',null=True) public_repos=models.CharField(max_length=100,default='nll',null=True) public_gists=models.CharField(max_length=100,default='nll',null=True) followers=models.CharField(max_length=100,default='nll',null=True) following=models.CharField(max_length=100,default='nll',null=True) created_at=models.CharField(max_length=100,default='nll',null=True) updated_at=models.CharField(max_length=100,default='nll',null=True)

dat = models.DateField() class sapi(models.Model): dat = models.DateField()` It was worked two days ago.. But suddenly it is not working..thanks

Could you post the exact date functions that you're using in your select? date('now', 'start of day') is not valid, so presumably you have something different there.

cur.execute("SElECT * FROM webapp_information WHERE dat BETWEEN date('now', 'start of day') AND date('now', 'localtime');")

It is the statement I used..

What to do now.. why it is not valid What are the other ways to do..

cur.execute("SElECT * FROM webapp_information WHERE dat BETWEEN date('now', 'start of day') AND date('now', 'localtime');")

It is the statement I used..

What to do now.. why it is not valid What are the other ways to do..

Ah, wait, you're using SQLite, aren't you. Sorry, for some reason I was assuming you were using MySQL, which has a different date function.

What happens if you start a SQLite session from the command line -- that is, you start a bash console and then cd to the directory containing the database file, then run

sqlite3 database.db

(replacing database.db with the real database filename, of course), and then run the query there?

hello, it is not working in the sqlite3 session but giving the date itself in quotes work.. but how to give the logic like 'give a month data'..

If it's not working in SQLite as well as in Django, your query is simply not returning any results. If I read it correctly, it's asking for all rows in the webapp_information table where dat is in the time period from the start of the current day to the current moment -- that is, if you ran it on Monday at 2pm, it would give you all rows for the period from midnight Monday to 2pm, and if you ran it today at 3:22pm, it would give you all rows for the period from midnight today to 3:22pm today.

If you want to get all rows with the dat field in the current month, then you'll need to adjust the parameters you're passing to the date function in your query; here's a link to the SQLite documentation page.