使用SQLiteOpenHelper的onUpgrade方法:
// 判断表中是否存在某个字段
private boolean isFieldExist(SQLiteDatabase db, String tableName, String fieldName) {
boolean isExist = false;
Cursor cursor = null;
try {
cursor = db.rawQuery("PRAGMA table_info(" + tableName + ")", null);
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex("name"));
if (name.equals(fieldName)) {
isExist = true;
break;
}
}
} finally {
if (cursor != null) {
cursor.close();
}
}
return isExist;
}