How to store the time in SQLite?
I have use the time as a static final
value. I was stuck on inserting and retrieving the time from and to SQLite.
I referred to some sites in Google:
SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values:
TEXT as ISO8601 strings ("YYYY-MM-DD HH: MM: SS.SSS"). REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. According to the proleptic Gregorian calendar. INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC. Applications can chose to store dates and times in any of these formats and freely convert between formats using the built-in date and time functions.
But I am confused.
And I am getting this error.
Caused by: android.database.sqlite.SQLiteException: near "From": syntax error (code 1): , while compiling: CREATE TABLE IF NOT EXISTS proname(ImageID INTEGER,Title TEXT,From INTEGER,To INTEGER); at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
public static final String[] titles = new String[]{"Akilam 360","Ipadikku Idhayam",
"Palsuvai Thoranam"};
public static final String[] fromtime = new String[]{"05:00:00","07:00:00","09:00:00"};
public static final String[] totime = new String[]{"07:00:00","09:00:00","11:00:00"};
public static final Integer[] images = {R.drawable.akilam_360,
R.drawable.ipadikku_idhayam, R.drawable.palsuvai_thoranam};
ListView listView;
List<Program> rowItems;
int iImageId;
String sTitle,sFrom,sTo ;
SQLiteDatabase db;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dbcon);
Calendar c = Calendar.getInstance();
int seconds = c.get(Calendar.SECOND);
int minutes = c.get(Calendar.MINUTE);
int hour = c.get(Calendar.HOUR_OF_DAY);
String time = hour+":"+minutes+":"+seconds;
timer = (TextView) findViewById(R.id.timer);
timer.setText(time);
db =openOrCreateDatabase("MukilProgram", Context.MODE_PRIVATE,null);
db.execSQL("CREATE TABLE IF NOT EXISTS proname(ImageID INTEGER,Title TEXT,From TEXT);");
rowItems = new ArrayList<Program>();
for (int i = 0; i < titles.length; i++) {
Program item = new Program(images[i], titles[i],fromtime[i],totime[i]);
rowItems.add(item);
}
db.execSQL("DELETE FROM proname;");
listView = (ListView) findViewById(listview);
final ProgramAdapter adapter = new ProgramAdapter(this,rowItems, false);
listView.setAdapter (adapter);
for (int i = 0; i < adapter.getCount(); i++) {
Program rowItem = (Program) adapter.getItem(i);
iImageId = rowItem.getImageId();
sTitle = rowItem.getTitle();
sFrom = rowItem.getFromtime();
sTo = rowItem.getTotime();
//sQuantity = rowItem.getQuantity();
db.execSQL("INSERT INTO proname VALUES("+ iImageId + ",'" + sTitle + "','"+sFrom+"','"+sTo+"');");
Toast.makeText(Databaseconnection.this, "Added to the Table..", Toast.LENGTH_SHORT).show();
}