My Android app has an SQLite database table with entries as follows:
USER_ID | NAME | MESSAGE | TIMESTAMP
where the timestamp is stored in a String in format:
"2012-10-10T03:14:32+0000"
I'm looking for a way to delete entries older than 3 days.
The table is likely to have less than 3,000 rows at a time.
I'm new to SQL and my question is how can I approach this in SQL for SQLite? Is there a less expensive way to do this in SQL than to iterate all posts in Java?
You can have a look at this doc.
Another way is to use the fact that the date format you use can be sorted directly as a string (lexicographical order).
In java code, you build the string representing the datetime threshold (Now minus 3 days)
And just compare the strings in SQL in the where clause
SELECT *
FROM MyTable
WHERE TIMESTAMP >= 'The date I built in Java'
Most of the time, Timestamps and datetimes are stored as ticks in SQLite, which involves int comparaison (much faster).
Related
I have an SQLite database within my Android application, which stores dates as integers. These integers are derived from a call to Java.util.Date.getTime();. I am trying to run a raw query of my database to get a Cursor to pass to a CursorAdapter and display in a ListView, but the date is stored as an integer as returned by getTime().
To keep my program simple, I would like to avoid using a SimpleArrayAdapter, and stick with the CursorAdapter.
Is it somehow possible to format the integer within the date colum as mm-dd-yyyy so that the column of the table, that the cursor is pointing to, contains properly formatted values rather than the integer that was returned by Java.util.Date.getTime(); when I added the item to the database?
SELECT strftime("%m-%d-%Y", date_col, 'unixepoch') AS date_col
Your code will work if it expects a result set column in that format called date_col.
EDIT: One thing you need to watch out for is that getTime uses milliseconds since 1970, while standard UNIX time (including SQLite) uses seconds.
The Java.util.Date.getTime(); method is returning an integer that represents the "unix time".
The simplest way to read this number as a date is by storing it as-is, and reading it using the following Sqlite query:
SELECT strftime('%m-%d-%Y', 1092941466, 'unixepoch');
which returns:
08-19-2004
If you need another format, you can use the strftime function to format is as you like, or any of the other date formats and functions available.
You'll have to, as Matthew Flaschen points out in a commend below, divide the date by 1000 before you are able to use them in this way. "Real" unix times are measured in seconds since the epoch, and Java.util.Date.getTime(); returns milliseconds since epoch.
SQLite uses static rigid typing. With static typing, the datatype of a value is determined by its container - the particular column in which the value is stored.
Any value stored in the SQLite database has one of the following storage class:
NULL
INTEGER
REAL
TEXT
BLOB
so I am not sure what you meant by but the date is stored as a long, unhelpful integer.
For more details please refer to Datatypes In SQLite Version 3. For further information on storing date/time in SQLite please refer to SQL As Understood By SQLite.
I hope this helps.
I have a database that contains upcoming births. The file has 5 records in it with due dates of (11-15-2016,10-15-2016, 12-14-2016, 12-13-2016 and 02-12-2017)
The query is
SQLiteDatabase db = dbhelper.getReadableDatabase();
String QueryPart1="SELECT inseminations._ID, inseminations.patient_id, inseminations.date_due ";
String QueryPart2="FROM inseminations ";
String QueryPart3="WHERE inseminations.date_due>=? ";
String QueryPart4="ORDER BY inseminations.date_due ASC";
String FinalQuery=QueryPart1+QueryPart2+QueryPart3+QueryPart4;
mCursor = db.rawQuery(FinalQuery ,howToFilter);'
howToFilter is
String howToFilter[]={ExpectedDateDue};
ExpectedDateDue is
ExpectedDateDue = new SimpleDateFormat("MM-dd-yyyy").format(new Date());
The query retrieves all records except for the one from 2017, the listview is sorted correctly, it's as if the query is only taking those records where the month is after the current month instead of the entire date.
Comparison operators on text in SQLite use lexicographic order. What this means, for example, is "01-01-2017" is less than "05-04-2016". SQLite has no indication that these strings represent moments in time that should be compared chronologically.
In general, I dislike using strings for timestamps in my databases for reasons like this. I much prefer to store timestamps as long values representing seconds (or milliseconds) since epoch. It's far easier to compare timestamps this way, and converting to and from date strings is simple enough either in SQLite or in Java.
If that's not feasible for some reason, then I suggest you alter the way you store your date strings in the database. Instead of MM-dd-yyyy, use yyyy-MM-dd. In this format, lexicographic comparisons will work; using the same example as before, "2017-01-01" is greater than "2016-05-04". Additionally, this format is acceptable as input to SQLite's various date and time functions, should you decide to use them.
If you can't alter the way the dates are stored, you will need to somehow convert them into a format that is comparable in the way you expect. You could use SQLite's substr() and || (concatenation) functions in the query to convert the date from MM-dd-yyyy to yyyy-MM-dd format. The code in this answer demonstrates this.
I am developing android application. One of my application table has "foo_date" as column name. and data type is TEXT. I have following data in my table
primary key | foo_date | foo_value
--------------------------------------
1 2013-3-1 100
2 2013-3-2 2
3 2013-3-4 100
4 2013-3-3 1000
my requirement is to get data for the current week(i.e. 2013-3-4 to 2013-3-10). When i write below query, i am getting all data except row # 3
select * from foo_tbl where foo_date between '2013-3-4' and '2013-3-10';
How do I write sqlite query so that it includes all data for current week?
Thansk
Chintan
Your problem is that you have stored the dates in a format that cannot be compared easily; the character 1 comes before 4, so 2013-3-10 compares less than 2013-3-4.
As documented, you should use the ISO date format yyyy-mm-dd. With constant field sizes, you can use a query with string comparisons that work:
SELECT * FROM foo WHERE foo_date BETWEEN '2013-03-04' AND '2013-03-10'
or use a date function to compute the week:
SELECT * FROM foo WHERE strftime('%W', foo_date) = '09'
First, store the dates as standard sqlite time string format (i.e. YYYY-MM-DD).
Then, you can use this:
select * from foo where strftime('%W', foo_date) == strftime('%W', 'now')
to select the rows with the same week number as now.
I have a .csv file that I turned into an SQLite database with the first column as a date and time (as a TEXT datatype because it's in the format 7/20/2011 12:00:00 PM), the second column as the subsecond (a REAL, like 0.142857), and the rest of the columns as data about that time. I will be using it as a read-only db in my Android app, in which the user selects a start time and an end time and then the data is displayed graphically for that time period.
This is my first time using a database and I'm not sure how to work with this. I believe I need a unique NUMERIC primary key for each row but simply labeling them by row number is a waste of space. The first column is not unique and is a TEXT datatype anyway, though if the subsecond was to be somehow incorporated then it would be unique. But I really can't re-write the database because it has 65534 rows... How do you suggest I access specific times in this database?
In Android you need a column named _ID in your database (else you'll face some issues later on). You will use that as the primary key.
Dates and times can be stored in SQLite in the form of a text column in the following format (See http://www.sqlite.org/datatype3.html):
TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS")
If your database is static, simply use a tool such as SQLite Database Browser to convert it to a format convenient for Android.
If your database is local and external(not remote), than you must have _id and another another table android_metadata which will hold the locale.
If your database was remote. Yes, you can it is only matter of speed if you are write, since you don't. Using WHERE clause will do the work.
Every date can be converted to numeric timestamp quite easy:
Date date = new Date();
long timestamp = date.getTime();
Date otherDate = new Date(timestamp);
Numbers are MUCH easier and faster to process than text fields. If you are completely sure, that you have unique data within column you can use it as primary key.
Importing csv file into table should be also easy:
//just open file in some known way and read it line by line
// we have declared String[] data, String line is line read from your csv somewhere earlier
// in code
data = line.split(",");
//here you have to process values, and execute insert statement
You have to create indexes on every column which will be used to search or order data. Please be also aware, that rows in table has no "default", "natural" or any other order. If you execute this same select statement twice you can get two totally different results in meaning of sorting. So simple select statement should look like that:
select
_id, other_colum_name, some_other_column
from
table_name
where
column_name = 5
and other_column_name = 3
order by
column_name;
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.