我正在尝试使用dataBAseHelper类来创建数据库文件,但我无法在数据文件中找到它(它仍然是空的)。在调试时,似乎没有调用onCreate方法。我尝试了不同的databaseHelper版本,但它没有用。 你可以在下面看到我的代码(MAinActivity和DataBaseHelper类):
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "tasks.db";
public static final String TABLE_NAME = "task_table";
public static final String ID_COL = "ID";
public static final String NAME_COL = "NAME";
public static final String CATEGORY_COL = "CATEGORY";
public static final String DURATION_COL = "DURATION";
public static final String DATETIME_COL = "DATETIME";
public static final String DESCRIPTION_COL = "DATETIME";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE_TASKS = "CREATE TABLE " + TABLE_NAME + "("
+ ID_COL + " INTEGER PRIMARY KEY AUTOINCREMENT ,"
+ NAME_COL + " TEXT, "
+ DATETIME_COL + " TEXT,"
+ DURATION_COL + " INTEGER,"
+ CATEGORY_COL + " TEXT,"
+ DESCRIPTION_COL + " TEXT " + ")";
db.execSQL(CREATE_TABLE_TASKS);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
public boolean insertData(Task task) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(NAME_COL,task.getName());
contentValues.put(CATEGORY_COL,task.getCategory().toString());
contentValues.put(DATETIME_COL,task.getDateTime());
contentValues.put(DURATION_COL,task.getDuration());
contentValues.put(DESCRIPTION_COL,task.getDescription());
long result = db.insert(TABLE_NAME,null ,contentValues);
if(result == -1)
return false;
else
return true;
}
public Cursor getAllData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from "+TABLE_NAME,null);
return res;
}
public boolean updateData(String id,Task task) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(ID_COL,id);
contentValues.put(CATEGORY_COL,task.getCategory().toString());
contentValues.put(DATETIME_COL,task.getDateTime());
contentValues.put(DURATION_COL,task.getDuration());
contentValues.put(DESCRIPTION_COL,task.getDescription());
db.update(TABLE_NAME, contentValues, "ID = ?",new String[] { id });
return true;
}
public Integer deleteData (String id) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(TABLE_NAME, "ID = ?",new String[] {id});
}
}
public class MainActivity extends AppCompatActivity {
DatabaseHelper data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
data = new DatabaseHelper(this);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.item,menu);
Intent intent = new Intent(MainActivity.this, addActivity.class);
// startActivity(intent);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.add:
Intent intent = new Intent(MainActivity.this,addActivity.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
}
你实际上必须对数据库做一些调用onCreate
方法的事情;而不是仅仅创建数据库帮助程序的实例(更具体地说,使用getWriteableDatabase
或getReadableDatabase
执行打开数据库的操作)。
所以尝试添加一行如data.insert(new task(paremateres_to_construct_the task);
或者,您可以更改databasehelper构造函数以调用其中一个方法,从而导致调用onCreate
方法。例如: -
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
this.getWriteableDatabase();
}
后者将创建一个空表,前者将创建一个表添加一行。两者都不是问题。