Android examples for Database:Table Create
create Table If Not Exist in SQLiteDatabase
import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.util.Log; public class Main{ public final static String TAG = ""; /* w w w . j ava 2 s . c o m*/ public static void createTableIfNotExist(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; } }