delete Table If Exist in SQLiteDatabase - Android Database

Android examples for Database:Table Create

Description

delete Table If Exist in SQLiteDatabase

Demo Code


import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class Main{
    public final static String TAG = "";
    /*from  www. j a v a 2s.c om*/
    public static void deleteTableIfExist(SQLiteDatabase db, String sql,
            String tableName) {
        if (tableIsExist(db, tableName)) {
            //            Log.d(TAG, "onUpgrade database, sql: " + sql);
            db.execSQL(sql);
        }
    }
    
    public static boolean tableIsExist(SQLiteDatabase db, String tableName) {
        boolean result = false;
        if (tableName == null) {
            return false;
        }
        Cursor cursor = null;
        try {
            //            db = this.getReadableDatabase();
            String sql = "select count(*) as c from Sqlite_master where type ='table' and name ='"
                    + tableName.trim() + "' ";
            cursor = db.rawQuery(sql, null);
            if (cursor.moveToNext()) {
                int count = cursor.getInt(0);
                if (count > 0) {
                    result = true;
                }
            }

        } catch (Exception e) {
            Log.e(TAG, "[tableIsExist method]error, e: ", e);
        }
        return result;
    }
}

Related Tutorials