Issues in storing and retrieving data from sqlite in android - android

I am trying to retrieve data from the database with the following query:
select * from Oil Where date(DDate)=date('2012-08-07');
where DDate is one of the column in the Oil table with datatype text.I get the Arrayindexoutofboundsexception. I found that it is not returning any value the count is 0.
can anyone help me with this.What i am actually trying is to do is store date value in the table.I couldn't find which datatype should be used to save date,from the sqlite site is that it doesn't have a particular datatype for date and time.

SQL Server comes with the following data types for storing a date or a date/time value in the database:
DATE - format YYYY-MM-DD
DATETIME - format: YYYY-MM-DD HH:MM:SS
SMALLDATETIME - format: YYYY-MM-DD HH:MM:SS
select * from Oil Where date(DDate)=date('2012-08-07');
can be
select * from Oil Where DDate='2012-08-07';

If you have DDate in the date format 'yyyy-MM-dd' then you can directly compare the date using
select * from Oil Where DDate='2012-08-07';
else if you have DDate in the date format 'yyyy-MM-dd HH:mm:ss' then the query looks like this
select * from Oil Where date(DDate)='2012-08-07';
If you are saving date in sqlite database and you have to perform date operations like sorting or using date() and time() functions then you have to use the 'yyyy-MM-dd HH:mm:ss' date format.
Datatype doesn't matter for sqlite.

Related

Order By datetime column SQLite

I'm using a sqlie database wich have a datetime column, i stored values of the datetime column with format "yyyy-MM-dd HH:mm" but when i want to select values from Database and order by the datetime column, it gives me a wrong order.
SELECT id_user FROM RDV WHERE id_user= ? ORDER BY datetime(Date) DESC
i added datetime(Date) just to check if it works but it didn't work also
Acually i made in error, tha dates where in format "dd-MM-yyyy HH:mm" and i must put them in format yyyy-MM-dd HH:mm
What is the column name?
If it's called Date, just remove the call to datetime():
order by Date desc (or the name of the field if different)

Convert column dateformat inline with sql query

I have stored date in a column of android sqlite database in YYYY/MM/DD format . Now I want to use julianday function in sql query to select special rows of database however julianday uses date in YYYY-MM-DD format . How can I convert date format inline with sql query?
Please rewrite this sample query which "startdate" and "enddate" are stored in YYYY/MM/DD format not YYYY-MM-DD:
select * from events where julianday(enddate)-julianday(startdate)>1200
SQL query accepts inline replace:
select * from events where julianday(replace(enddate,'/','-'))-julianday(replace(startdate,'/','-'))>1200
for more complex conversion use prepared SQL convert() function

Sort SQLite database by date only in android

I have a SQlite database with date stored as string datatype (dd-mm-yyyy format). How can I sort the rows in my database by date ?
The Query for sorting date is:
SELECT EventDate,Event,ID from EventCalenderTable Order By EventDate ASC
Note:
SQLite only knows three date formats:
Text ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS")
Real Julian day numbers since November 24, 4714 B.C
Integer number of seconds since 1970-01-01 00:00:00 UTC
SQLite does have five date/time functions for converting between formats.

Datetime aritmethics in sqlite on android

I am trying to do some datetime aritmethics in Sqllite. I have a table with two rows defined as type DATETIME. These fields are called LOG_FROM and LOG_TO. In a query on this table I try to do the following:
SELECT SUM(LOG_FROM - LOG_TO) AS TIMESPENT FROM LOG_TABLE WHERE X=Y;
This subtraction inside the SUM function does not work as intended. It will always return the number 0. I read the data via a cursor.getString.
When I store dates in the table I convert them to strings on the format yyyy-MM-dd HH:mm:ss
SQLite ignores column data types. Your date strings are strings.
Your date format is supported by the built-in date functions, so you can use them to convert the dates to numbers.
Assuming that you want the result to be a number of days, use julianday:
SELECT SUM(julianday(LOG_TO) - julianday(LOG_FROM)) ...

how to do date comparision through sql query?

how to compare dates .
for example my date format is 2011/10/12(YYYY/MM/DD).
I'm having some date records in database .i want some record greater than particular date.
for example :
String compareDate=2011/10/16;
select * from testtable where date>compareDate;
how to get least date and highest date through database query.
in sqlite is there any method to compare dates.
Thanks in Advance
Push the date to query in right format
select * from testtable where date > '2011-10-16';
more
Mysql Compare two datetime fields
http://dev.mysql.com/doc/refman/5.0/en/using-date.html
http://www.java2s.com/Code/SQL/Date-Time/Comparedateinwhereclause.htm
Before going to execute query, compareDate should be in this ("yyyy-MM-dd") format,
then try this query
select date from testtable where date > 'compareDate' order by date desc;
the above query order the date by descending order. The first row of cursor will have the max date.

Categories

Resources