I want to hold only last 22 minute data in my DB for that i used this
String[] str = null;
str = new String[]{"datetime('now','-22 minute')"};
databaseInstance.delete(table_n, "datetime(modified/1000,'unixepoch')<= ?", str);
In database our datatype of modified column is as below
"modified" DATETIME NOT NULL
But it deleting all data from DB that means either of before 22 minute or after 22 minute all data get deleted from our table. But i want to hold last 22 minute data in our table .Any help is really appreciated, Please someone take me out of this issue.Thanks in advance to all.
The while point of using parameters (?) is to avoid interpreting them, so that you are able to store strings containing quotes or function names in the database.
To execute the datetime function, it must be written directly into the SQL statement:
db.delete(table_n, "datetime(modified/1000,'unixepoch') <= datetime('now','-22 minute')", null);
Instead of converting the table value into the yyyy-mm-dd hh:mm:ss format, you could convert your comparison timestamp into the same format used in the table.
This would allow an index on the modified column (if any) to be used:
db.delete(table_n, "modified <= strftime('%s','now','-22 minutes')*1000", null);
Related
I'm comparing two dates in android sqlite database
I stored dates in the format
YYYY-MM-DD
Cursor cursor=db.rawQuery("SELECT * FROM "+tableName+" WHERE SALESDATE BETWEEN '2020-1-01' AND '2020-2-01';",null);
It gives result with dates of month 10, 11 and 12 along with the dates between above specified dates.
I would like to know if it is a bug or is there any mistake in my code.
The problem here is that your date literals are in a non standard (and likely incorrect) format. Appreciate that the following inequality holds true:
'2020-10-01' > '2020-1-01' AND '2020-10-01' < '2020-2-01'
This is true because the text 10 is lexicographically larger than just 1, but also less than 2. To avoid this problem, use proper date literals:
String sql = "SELECT * FROM " + tableName + " WHERE SALESDATE BETWEEN '2020-01-01' AND '2020-02-01';"
Cursor cursor = db.rawQuery(sql, null);
Note that SQLite does not actually have a formal date type. Thus, it is very important to always store your dates in SQLite using a proper ISO format.
You should store your Date as long value in database. Simple new Date().getTime() gives you this value and new Date(long value) returns it back. So you can make such queries easy.
But what I can suggest is to:
Export your table to CSV,
Change the date values to a proper SQLite TimeString and
Re-import the CSV after deleting the original table.
Then, you can run a query like:
SELECT * FROM tableName WHERE SALESDATE BETWEEN '2020-01-01 00:00:00' AND '2020-02-01 23:59:59'
Im using SQLite as data storage for my Android App. I have created one table with column of type datetime. When I do the insert of records or selects statements I use the format dd.MM.yyyy (10.08.2012) for dates.
Now, I have issue with getting the row with the latest / MAX date. I have tried using the MAX(date_column) statement, but it returns wrong date back. I have following dates in my table and it returns 31.07.2012 in stead of 04.08.2012. Anyone know what I'm doing wrong?
04.08.2012
03.08.2012
02.08.2012
01.08.2012
31.07.2012
30.07.2012
Here is part of the code:
String selection = "measurementDate = (SELECT MAX(measurementDate) FROM Measurements)";
Cursor cursor = database.query("Measurements", allColumns, selection, null, null, null, null);
cursor.moveToFirst();
...
Could it be because of 31?
Try to see if this helps:
SELECT measurementDate FROM Measurements ORDER BY measurementDate desc LIMIT 1
It's probably due to the comparison being made on the stored string rather than treating it as a date.
You could store the string in differently, YYYYMMDD, which should compare them correctly or, if you can modify the table, store the dates as milliseconds and format back to a string when needed.
This might help
I've added 3 strings of data into a SQL database in android. A fourth string makes up the database, however it is a date:
SimpleDateFormat outputDate = new SimpleDateFormat(yyyy-MM-dd)
Date outputDate = new Date();
When the user enters information to the database, the adding method for the database creates the date, which is added to the database.
My question is, how can I use these imported dates to create a method whereby only the rows in the database that were added on in the last 31 days?
A SELECT method comes to mind through research, but I don't know how to implement it?
Thanks
Presuming your database engine is SQLite, you should try something like that
SELECT * from your_table where julianday('now') - julianday(your_date_field)<=31;
You may be interested in SQLite date functions as well: http://www.sqlite.org/lang_datefunc.html
As long as you ask about Android I assume that the database you use is SQLite. SQLite does not have datatype for date. Thus you have couple of options: store the date in string (like you did) or store the timestamp of the date.
I seriously recommend you to use the second option as it is going to convert your dates in integers and it will be even easier to handle.
Thus when you want to select only events in the last 31 days you can do:
SQLiteDatabase database = helper.getWriteableDatabase();
final long millisIn31Days = 31 * 24 * 60 * 60 * 1000;
String where = "<your-date-column> >= ?"
String [] whereArgs = { String.valueOf(new Date().getTime() - millisIn31Days) };
Cursor cursor =
database.query("<TABLE_NAME>", null, where, whereArgs, null, null, null);
Note that here I write in angular brackets all the values you should actually fill in with the correct constants.
If you, however, want to stick to the string solution, you can still go with string comparison, as long as you specify the date format in correct manner (yours seems to be like that). In the string solution you will need to calculate the date of 31 days ago. You can use the Calendar's class auxiliary methods to achieve that.
I have a column in android sqlite database table. The values in the column are like:
2011/06/01
2011/06/02
2011/06/05
2011/06/10
2011/06/11
2011/06/13
2011/06/15
2011/06/16
2011/06/25
2011/06/26
I have a string today="2011/06/27"
Now I want to delete those rows whose column value is older than 5 days from today.
How to modify the code to achieve this?
return db.delete(DATABASE_TABLE,where date="", null) > 0;
You have to store your dates in a other format, check the date function of SQLite. Use YYYY-MM-DD instead of YYYY/MM/DD. You already got an order on your dates defined by its string representation. So you can use the date function of SQLite to select the correct rows.
Dates older than 5 days would be queried like this.
... WHERE datecolumn < date('now', '-5 days') ...
There is no date type in sqllite
you just store a data represented value as a string or int
then use the date based functions of sqllite to get the values
You should be using the stored procedure rather than writing the service. It will make easier for your application to automatically update the records.
See link: http://searchoracle.techtarget.com/answer/Time-based-stored-procedure-to-check-table-and-update-old-records
Another solution is to use date instead of string. When you run your service then fetch data as Date and then you can make the simple comparison.
I am using SQLite database for my application.
The Table Structure Goes Like :
_id : integer primary key
name : text
day : date
I am able to store date in format : dd-mmmmm-yyyy eg. 15-June-2011
But when i tried to retrieve all records filtered by date from the database it returns me null.
database.query(DATABASE_TABLE, new String[] { "strftime('%d-%mm-%Y',date('now'))","strftime('%d-%m-%Y',"+KEY_DAY+")" },
"strftime('%d-%m-%Y',date('now'))=" + KEY_DAY , null,null,null,null,null);
It didnt match with anyrow's date even though there were some matching dates.
I have already gone thru documentation of SQLite. But didn find any solution yet.
I want to have something like :
select * from table where day=curdate();
How can i do the same task in SQLite ?.
(Yes I am flexible to change the format of date stored in Dateabase)
What are other alternatives for the same task ?.
In java programming you can convert any date format into long (time in milliseconds) and viceversa. My opinion is while storing format the date into long format in java and then store long value of date in database. also while retrieving you can retrieve the long value and then format that as per your expected date format. I have been using this type of logic for several application.
Thanks
Deepak.
The function strftime('%d-%m-%Y',date('now')) returns a string with the month in numeric format (from 01 to 12). As far as I can tell from the docs, there is no format specifier to return the full name of the month.
I think you'll have to store your dates using numerical month specifiers instead of names.