I have this query in SQLite that I want to return a value based on the date passed. The date is in the database as a String. I know that there is data in the database with that date, but it just returns nothing. I dont know why. When I make the query without comparisson parameters, it returns all the data normally, but using the WHERE clause in the query method, it stops returning. Maybe its just a matter of the SQL syntax. Anyways, here is the statement:
Cursor cursor = db.query(TABLE, new String[] {"_id", "value"}, "current_date = ? ", new String[] {date}, null, null, null);
EDIT
I've made this query in SQLite Database Browser, and it doesn't return any values. Even if its there.
I've seen that this is quite funny and confused. I've tested on Database Browser and also on phone, when I pass the current day, the 'Today' day, it gets the data, but when I pass another day that isnt today, it returns nothing. Very, very weird.
And in Database Browser, I've selected the column that contains the date and it returns only the 'Today' date, even if before there are another dates.
if your date is stored in database in format 'dd-mm-yyyy',then try using it like-
Calendar cal=Calendar.getInstance();
Cursor cursor = db.query(TABLE, new String[] {"_id", "value"}, "date = ? ", String.format("%1$td-%1$tm-%1$tY",cal), null, null, null);
For other formats,please refer http://download.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html
The column name was 'current_date' and this is a SQLite function name. So it didn't get the other dates, just the current date.
Related
I want to write a Where clause for Sqlite db & my query is as follows,
Cursor cursor = database.query(table_name,new String[]{COLUMN_1,COLUMN_2,COLUMN_3}, COLUMN_1='1', null, null, null, null);
//COLUMN_1='1' is my WHERE Clause & its datatype is text
I am not able to execute this query & its giving Nullpointer exception immediately after this statement.
I dont know the reason I think there is some problem with text datatype.
I have spent almost half a day searching for the solution but disappointed.
PS: I've also tried using ,
Cursor cursor = database.query(table_name,new String[]{COLUMN_1,COLUMN_2,COLUMN_3}, COLUMN_1=?, new String[] {'1'}, null, null, null);
LogCat:
But Same problem.
Do you initialize the database? Do you have anything like this:
database = new DBAdapter(this);
Also you need to open the database before the query.
database.open();
I have found the problem, the database was not opened & hence giving NPE. Thank you guys for your patience.
Since the db does not have create date and some ordering field (but in my observation the last row is the latest record),
so how can i get the five last record in some condition e.g.
five record that their schoolid == 1?
Thanks
public Cursor select()
{
String orderBy = FIELD_pubKey+" DESC";
Cursor cursor = iReadDatabase.query(TABLE_NAME, null, null, null, null, null, orderBy);
cursor.moveToFirst();
return cursor;
}
There's no such thing as a last record or a first or a 42nd.
Which records appears last in the result of a query is dependent on the query plan, or an Explicit order by if you add one.
Select * From Table Where ...
The rows will be returned in whatever order the engine considers suitable at the time.
If you need them in specific order, then add an order by clause to the query, anything else is asking for it.
Something like
Select * From Table Order by SomeColumn desc limit 5
will do what you require.
Now what column you need to order by I've no idea, but you need one that will do the job, assuming automatic primary key, but note it is possible to mess with that.
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 have a listview populated from an SQLite database. I have several items that I successfully populate into the listview, however I'm having trouble with one last thing.
I'm trying to queue the sum total of the column KEY_CONTENT6 which is a string type, however it only contains numbers. I'd like to keep it as a string, so to add it up I'm using Double.valueOf(). The problem is this code force closes on queue and I cant figure out whats wrong:
public Cursor queueAll(){
String[] columns =
new String[]{KEY_ID, "sum("+ Double.valueOf(KEY_CONTENT6) +")",
KEY_CONTENT9, KEY_CONTENT10 };
Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns,
null , null, KEY_CONTENT10, null, KEY_CONTENT9+ " DESC");
return cursor;
}
simply use SUM, no need to use anything else..
String[] columns =
new String[]{KEY_ID, "sum(KEY_CONTENT6)",
KEY_CONTENT9, KEY_CONTENT10 };
It is valid for SQLite. Because, no matter what you set data type in SQLite, it stores values as string. So, type conversion is somewhat built-in in SQLite.
You can't use java in a SQL statement, either stick to strait sql or iterate over the cursor and use java to do your calculation.
You can find everything there is to know about sqlite here http://www.sqlite.org/docs.html
SQLite is basically typeless, so you might be able to use SUM on your column even though it is a string. However, if it's meant to be a numeric column, why not give it a number type??
I have "date"(TEXT) column in db.I want to display dates in ascending order(i.e. group by date).but according to current date.
i.e dates greator or equal than current date should be displayed first and past dates should get appended at last in listview automatically.I used SimpleCursorAdapter to display list.
So how should I do this?
Write the proper query and you should be fine. If you are looking for that particular query as an answer to your question, tag it as sql or sqlite as well.
You can sort data items when you try to query..here a cursor object set with parameters(just an example),the 7th parameter With "ASC" means that the return data will be in ascending oder
cursor cur = yourdata.query(TableName, coloumns, null, null,null, null, table.COLUMN_NAME + " ASC");
if you want an example you can try this code example
Click me