I am writing an Android app that allows the user to add a new Module to the database and add the day, starttime, finish time and room for that module.
The user is able to view all classes for a particular day.
The problem i am having is, right now i am currently taking the start time and finish time as text because i couldnt find out how to take it in as a time.
1) The user clicks the EditText, a TimePicker Dialog pops up, the user selects the hour and minute and the EditText text is set to the time picked.
2) The text in the EditText is then taken and saved into the database.
I have everything working fine, the only problem i am facing is that when the user is viewing all the classes for a particular day i want it to be ordered by start time i.e. the first class, the second and so on. But right now its only showing it in the order it was inserted into the database. I have tried the ORDER BY and it just wouldnt work.
I am guessing i would have to change the data type of the start time and finish time.
db.execSQL("CREATE TABLE AllModulesInfo (_id INTEGER PRIMARY KEY AUTOINCREMENT, moduleName TEXT, day Text, startTime Text, finishTime Text, room Text);");
public void insertIntoAllMod(String moduleName, String day, String startTime, String finishTime, String room)
{
ContentValues daysInfo = new ContentValues();
daysInfo.put("moduleName", moduleName);
daysInfo.put(Day, day);
daysInfo.put(StartTime, startTime);
daysInfo.put(FinishTime, finishTime);
daysInfo.put(Room, room);
db.insert("AllModulesInfo", null, daysInfo);
}
Thanks in advance, if there is anything i havent explained properly or any information i have left out that will assist you in assiting me to fix this problem, PLEASE let me know. Thanks
It looks like you’re using strings for your timestamps. You need to use an integer to actually store a unix timestamp. Then your queries will using ORDER BY will sort correctly, ex:
I’d suggest converting the string to an integer using straight Java, since that allows you to use those parameterized inserts like you showed in your example. However, you could manually write the queries and use the time functions provided by SQLite:
http://www.sqlite.org/lang_datefunc.html
Edit:
It appears you can use a string timestamp with SQLite, but only a few formats are supported (that page has examples.) I’m not sure that works in Android though, I’ve always used integer field types for times (which can actually store Java longs.)
— Dan