Mac OS-X
Android
Eclipse with ADT
SQLite
I'm new to creating Android Apps and Ive researched this heavily but I'm getting nowhere. I need to query my SQLite database to return all the rows between 2 dates. What I have learnt from my research so far is that there is no DateTime column in Androids SQLite database and I have to save it as a text column. But I think the problem lies with trying to compare Strings but I can't figure out a way around it. Here is my code :
DB = currentContext.openOrCreateDatabase(DBName, 0, null);
DB.execSQL("CREATE TABLE IF NOT EXISTS " + tableName + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, Date VARCHAR(40), Hours INT(3));");
I am not getting any errors but I am not returning any results from my RawQuery. Here is my code:
Cursor c = newDB.rawQuery("select ID, Date, Hours from " + tableName + " where Date BETWEEN '" + date1 + " 00:00:00' AND '" + date2 + " 99:99:99'", null);
if (c != null ) {
Log.d(TAG, "date1: "+date1);
Log.d(TAG, "date2: "+date2);
if (c.moveToFirst()) {
do {
int id = c.getInt(c.getColumnIndex("ID"));
String date1 = c.getString(c.getColumnIndex("Date"));
int hours1 = c.getInt(c.getColumnIndex("Hours"));
results.add(+ id + " Date: " + date1 + " Hours: " + hours1);
}
while (c.moveToNext());
}
}
I have also tried the following statement but they do not yield results either:
Cursor c = newDB.rawQuery("select ID, Date, Hours from " + tableName + " where Date BETWEEN " + Date(date1) + " AND " + Date(date2) + "", null);
I got this statement from other StackOverflow answers but I cannot find any documentation on the date() method. Im not sure whether it is obsolete now but I get error saying "Date(String) method is undefined for the type (classname)" and I have tried importing some JAVA Libraries.
Does anyone know the correct way to create a rawData() query that will get the rows between selected dates??
Further info, these are my INSERT statements and the date format that goes into the database:
newDB.execSQL("INSERT INTO " + tableName + " (Date, Hours) Values ('" + ph_date + "'," + hours + ");");
String date1 = "12/1/13"
String date2 = "25/1/13"
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…