Android examples for Database:Table Drop
Delete table from database by class associated with class.Class must be marked by DatabaseTable annotation.
import android.database.sqlite.SQLiteDatabase; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Date; public class Main{ /**/* w ww .ja v a 2 s . co m*/ * Delete table from db by class associated with class.Class must be marked * by {@link DatabaseTable} annotation. * * @param db * @param mappedClass */ public static void deleteTable(SQLiteDatabase db, Class<?> mappedClass) { if (db == null) { throw new IllegalArgumentException("db should not be null"); } String sql = deleteTableQuery(mappedClass); db.execSQL(sql); } /** * Delete table from db by class associated with class.Class must be marked * by {@link DatabaseTable} annotation. * * @param mappedClass */ public static String deleteTableQuery(Class<?> mappedClass) { if (mappedClass == null) { throw new IllegalArgumentException( "mappedClass should not be null"); } String tableName = getTableName(mappedClass); return "DROP TABLE IF EXISTS " + tableName; } /** * Get name of table by class marked {@link DatabaseTable} annotation. * * @param mappedClass * @return - name of class. */ public static String getTableName(Class<?> mappedClass) { if (mappedClass == null) { throw new NullPointerException( "\"mappedClass\" should not be null"); } DatabaseTable classAnnotation = mappedClass .getAnnotation(DatabaseTable.class); if (classAnnotation == null) { throw new IllegalArgumentException( "Class mut be marked by DatabaseTable anatation"); } String tableName = classAnnotation.name(); if (tableName == null || tableName.length() <= 0) { tableName = mappedClass.getSimpleName(); } return tableName; } }