I am making my first android project. Its purpose is currently to store times when a button is pressed, in a database. The full contents of the database are then displayed using a LoaderManager / CursorLoader / Custom SimpleCursorAdapter / ContentProvider / ListView based activity.
The problem is this: the database is not storing any numbers in the _id column when I insert a new row. It is just blank:
I have tried to find an answer to this to no avail, and I tried to debug deep into the writing-to-database code, to no avail.
The code that executes when the button is pressed is:
// Function to record an awake time
public void onAwake(View view) {
DateTime now = new DateTime();
DateTime startOfCurrentDay = now.withTimeAtStartOfDay();
DateTime noonOfCurrentDay = startOfCurrentDay.plusHours(12);
TimeEntry timeEntry = new TimeEntry(noonOfCurrentDay.getMillis(), now.getMillis(), TimeEntry.TimeEntryType.TIME_RECORD, TimeEntry.Direction.WAKE);
TimeEntryDbHandler timeEntryDbHandler = new TimeEntryDbHandler(this);
long id = timeEntryDbHandler.addTimeEntry(timeEntry);
// Notify user of execution
Toast.makeText(this, R.string.toast_awake + " ID:" + String.valueOf(id), Toast.LENGTH_SHORT).show();
}
When this executes, the ID that is shown in the toast is incrementing as you would expect (currently at 32 or something).
The database handler code is then as follows:
public class TimeEntryDbHandler extends SQLiteOpenHelper implements BaseColumns {
// If you change the database schema, you must increment the database version.
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "FeedReader.db";
public static final String TABLE_TIME_ENTRIES = "time_entries";
//public static final String _ID = "_id"; // now in basecolumns
public static final String COLUMN_CENTER_OF_DAY = "center_of_day";
public static final String COLUMN_TIME = "time";
public static final String COLUMN_TIME_ENTRY_TYPE = "time_entry_type";
public static final String COLUMN_WAKEUP_OR_BEDTIME = "wakeup_or_bedtime";
public TimeEntryDbHandler(Context context) {
super (context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TIME_ENTRIES_TABLE = "CREATE TABLE " + TABLE_TIME_ENTRIES + "("
+ _ID + " INTEGER PRIMARY KEY,"
+ COLUMN_CENTER_OF_DAY + " INTEGER,"
+ COLUMN_TIME + " INTEGER,"
+ COLUMN_TIME_ENTRY_TYPE + " TEXT,"
+ COLUMN_WAKEUP_OR_BEDTIME + " TEXT"
+ ")";
db.execSQL(CREATE_TIME_ENTRIES_TABLE);
}
// Method to add new time entry row to time entries table
public long addTimeEntry(TimeEntry timeEntry) {
ContentValues values = new ContentValues();
values.put(COLUMN_CENTER_OF_DAY, timeEntry.get_centerOfDay());
values.put(COLUMN_TIME, timeEntry.get_time());
values.put(COLUMN_TIME_ENTRY_TYPE, timeEntry.get_timeEntryType().name());
values.put(COLUMN_WAKEUP_OR_BEDTIME, timeEntry.get_direction().name());
SQLiteDatabase db = this.getWritableDatabase();
long newRowId;
newRowId = db.insert(TABLE_TIME_ENTRIES, null, values);
db.close();
return newRowId;
}
Does anyone know why the database is not simply getting an incrementing integer written into the _id column? Otherwise, what do you suggest I do to debug this?
I also tried the 'AUTO INCREMENT' directive in the onCreate SQL command, but that didn't change anything.
If other areas of the project code need to be posted, let me know what it is and I'll post it.
Many thanks!
Aucun commentaire:
Enregistrer un commentaire