当我创建数据库,第一个表(TABLE_CONTACTS)及其相关字段时,代码运行良好。
但是一旦我在Helper Class中的onCreate()方法下创建了另一个表(TABLE_CATEGORIES)。
它开始显示第二个表未创建..并且弹出了许多与未创建数据库相关的异常。
以下是CODE:
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 2;
// Database Name
private static final String DATABASE_NAME = "imagedb";
// Contacts table name
private static final String TABLE_CONTACTS = "contacts";
// Categories table name
private static final String TABLE_CATEGORIES = "categories";
// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_IMAGE = "image";
// Categories Table Columns names
private static final String KEY_ID1 = "id";
private static final String KEY_NAME1 = "name";
private static final String KEY_IMAGE1 = "image";
public DataBaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
final String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_NAME + " TEXT NOT NULL UNIQUE,"
+ KEY_IMAGE + " BLOB" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
final String CREATE_CATEGORIES_TABLE = "CREATE TABLE " + TABLE_CATEGORIES + "("
+ KEY_ID1 + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_NAME1 + " TEXT NOT NULL UNIQUE,"
+ KEY_IMAGE1 + " BLOB" + ")";
db.execSQL(CREATE_CATEGORIES_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CATEGORIES);
// Create tables again
onCreate(db);
}
LOGCAT ERROR LOG:
12-20 13:19:33.674: E/AndroidRuntime(8677): FATAL EXCEPTION: main 12-20 13:19:33.674: E/AndroidRuntime(8677): java.lang.RuntimeException: Unable to start activityComponentInfo{com.example.android.searchabledict/com.example.android.searchabledict.CategoriesDemoActivity}: android.database.sqlite.SQLiteException: no such table: categories (code 1): , while compiling: SELECT * FROM categories ORDER BY name1