jeudi 30 juillet 2015

Android SQLite Possible Bug: Inserting Date

Here is some code from a database handler class to manage an SQLitedatabase table. The date is inserted as 04082015, but outputs as 40082015, this error does not occur for dates were the day is larger than 10.

I'm creating the table as follows

@Override
public void onCreate(SQLiteDatabase db) {
        String create_college_table = "CREATE TABLE " + TableName + " (" +
                "  `"+KEY_ID+"` INTEGER PRIMARY KEY AUTOINCREMENT,\n" +
                "  `"+KEY_DATE+"` DATETIME NOT NULL UNIQUE,\n" +
                "  `"+KEY_JSON+"` longtext NOT NULL\n" +
                ")";
        db.execSQL(create_college_table);
}

Now, inserting a row into the above table:

public void testit() {
    // write the row into the database:
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues insertValues = new ContentValues();
    // DATE IS FOURTH OF AUGUST 2015
    insertValues.put(KEY_DATE, "04082015");
    insertValues.put(KEY_JSON, "{test}");
    db.insert(
            TableName,
            null,
            insertValues
    );


    // Now read from the database, select the row that was
    // just inserted
    String selectQuery = "SELECT * FROM " + TableName + " " +
            "WHERE " + KEY_JSON + " = '{test}'";
    SQLiteDatabase db_read = this.getWritableDatabase();
    Cursor cursor = db_read.rawQuery(selectQuery, null);
    // Viewing the log the inserted date (04082015) has been
    // outputted as 40082015, this does not occur with dates
    // were the day in the month is ten or larger?!
    Log.e("testMethod", DatabaseUtils.dumpCursorToString(cursor));
}

Aucun commentaire:

Enregistrer un commentaire