I'm using Android Studio to make a simple log app where the user can add their time and note which are supposed to saved into an SQLite database. However, when I try to find the database in the Android device monitor/file explorer, it doesn't appear in the folder named "data" (where one of you guys has suggested it's supposed to be saved). In on of the questions here on SO it was also stated that the database won't appear till the getWritableDataBase()
or getReadableDataBase()
is executed. However, even with that I can't find it, the data folder is still empty. I've tried to uninstall and install again, that also didn't work.
TimeListDataBaseHelper.java
public class TimeListDataBaseHelper {
public static final int DATABASE_VERSION=2;
public static final String DATABASE_NAME="TimeTracker.db",TABLE_NAME="time_records",TIMETRACKER_COLUMN_ID="id",TIMETRACKER_COLUMN_TIME="time",TIMETRACKER_COLUMN_NOTE="note";
private TimeTrackerOpenHelper timeTrackerOpenHelper;
public SQLiteDatabase database;
public TimeListDataBaseHelper(Context context)
{
timeTrackerOpenHelper=new TimeTrackerOpenHelper(context);
database=timeTrackerOpenHelper.getWritableDatabase();
}
private class TimeTrackerOpenHelper extends SQLiteOpenHelper
{
public TimeTrackerOpenHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + "(" + TIMETRACKER_COLUMN_ID + " INTEGER PRIMARY KEY," + TIMETRACKER_COLUMN_TIME + " TEXT," + TIMETRACKER_COLUMN_NOTE + " TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
database.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
onCreate(db);
}}
public void saveTimeRecord (String time,String notes)
{
ContentValues contentValues=new ContentValues();
contentValues.put(TIMETRACKER_COLUMN_TIME,time);
contentValues.put(TIMETRACKER_COLUMN_NOTE,notes);
database.insert(TABLE_NAME,null,contentValues);
}}
This is the part of the MainActivity.java which I think is related to my question, the thing is that the log is indicating that the database is created because I wrote an if statement before the Log.d.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timeListDataBaseHelper=new TimeListDataBaseHelper(this);
if(timeListDataBaseHelper.database!=null)
Log.d("tag","Data Base Created");
ListView listView=(ListView) findViewById(R.id.time_list);
timeTrackerAdapter=new TimeTrackerAdapter();
listView.setAdapter(timeTrackerAdapter);
}
Aucun commentaire:
Enregistrer un commentaire