Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get string, date, time and int from sqlite database in android

Tags:

android

sqlite

i inserted a string, date, time and int value into an sqlite database using this code

void addRemAction(RemActions remaction) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(KEY_ACTOINS_TITLE, remaction.getTitle()); // Action title
    values.put(KEY_ACTIONS_MSG, remaction.getMsg()); // action msg



    values.put(KEY_ACTIONS_DATE, dateFormat.format(new Date()));  // action date
    values.put(KEY_ACTIONS_TIME, remaction.getTime()); // action time
    values.put(KEY_ACTIONS_PLACE_LONG, remaction.getPlong()); // action place long
    values.put(KEY_ACTIONS_PLACE_LAT, remaction.getPlat());  // action place lat

    // Inserting Row
    db.insert(TABLE_ACTIONS, null, values);
    db.close(); // Closing database connection

and this is my code to retrieve it

RemActions getRemAction(int id) {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(TABLE_ACTIONS, new String[] {KEY_ACTOINS_TITLE, KEY_ACTIONS_MSG, KEY_PLACES_NAME, KEY_ACTIONS_DATE, KEY_ACTIONS_TIME}, KEY_ACTIONS_ID + "=?",
            new String[] { String.valueOf(id) }, null, null, null, null);
    if (cursor != null)
        cursor.moveToFirst();

    RemActions remActions = new RemActions(cursor.getString(0),
            cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getInt(4));
    // return remActions
    return remActions;

now my ide is giving me errors because the 4th argument in my constructor for the RemAction class is of type Date from the java.util.date class.

My question is "how do i set the cursor to get it in a date format?"

ill b glad to provide additional code on request

like image 882
CtJnx Avatar asked Jul 30 '13 11:07

CtJnx


1 Answers

Since you cannot save date/time values in SQLite, you should first convert them to milliseconds and store them as that.

Date c = new Date(System.currentTimeMillis());
long milliseconds = c.getTime();
//To get current time

If you then need to change milliseconds back to a date format:

Date c = new Date(cursor.getLong(4));

Then you can format the date using SimpleDateFormat.

like image 54
ClaireG Avatar answered Sep 18 '22 12:09

ClaireG