is database Table Exist by counting - Android Database

Android examples for Database:Table Exists

Description

is database Table Exist by counting

Demo Code


//package com.book2s;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class Main {

    public static boolean isTableExist(SQLiteDatabase db, String tableName) {
        boolean result = false;
        if (tableName == null) {
            return false;
        }//from   ww  w  .j a v a 2 s. c om
        try {
            String sql = "select count(1) as c from sqlite_master where type ='table' and name ='"
                    + tableName.trim() + "'";
            Cursor cursor = db.rawQuery(sql, null);
            if (null != cursor) {
                if (cursor.moveToNext()) {
                    int count = cursor.getInt(0);
                    if (count > 0) {
                        result = true;
                    }
                }
                cursor.close();
            }
        } catch (Exception e) {
        }
        return result;
    }
}

Related Tutorials