Android:sqlite判断某个表里是否有某个字段

使用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;
    }

你可能感兴趣的:(Android,1024程序员节,android,sqlite)