请检查一下,有两个问题,第一个是这个数据库没有创建,第二个是这个toast给了我这个错误。
public static final String KEY_ID="id";
public static final String KEY_NAME="Name";
public static final String KEY_IMAGE = "levelone";
public DataBaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "(" + KEY_ID +
" INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_IMAGE + " BLOB" + ")";
sqLiteDatabase.execSQL(CREATE_TABLE);
Toast.makeText(DataBaseHandler.this,"sadads",Toast.LENGTH_LONG).show();
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(sqLiteDatabase);
}
答案 0 :(得分:0)
- 数据库没有创建原因?
醇>
首次拨打getWritableDatabase()
或getReadableDatabase()
时创建的数据库。因为DataBaseHandler
只是创建帮手。
DataBaseHandler helper = new DataBaseHandler(context);
helper.getWritableDatabase();
- Toast没有显示原因?
醇>
Toast需要显示上下文。您不能使用SqliteOpenHelper
DataBaseHandler
作为上下文。但是您可以将构造函数中传递的上下文用于getApplicationContext()
。
public static final String KEY_ID = "id";
public static final String KEY_NAME = "Name";
public static final String KEY_IMAGE = "levelone";
private Context context;
public DataBaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "(" + KEY_ID +
" INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_IMAGE + " BLOB" + ")";
sqLiteDatabase.execSQL(CREATE_TABLE);
Toast.makeText(context.getApplicationContext(), "sadads", Toast.LENGTH_LONG).show();
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(sqLiteDatabase);
}