-
-
Save chinhvo/9145810193c0b606e56cb13b8cc82fb7 to your computer and use it in GitHub Desktop.
Check table if exists on SQLite
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Check table if exists on SQLite | |
// http://stackoverflow.com/questions/3058909/how-does-one-check-if-a-table-exists-in-an-android-sqlite-database | |
// | |
public boolean isTableExists(String tableName, boolean openDb) { | |
if(openDb) { | |
if(mDatabase == null || !mDatabase.isOpen()) { | |
mDatabase = getReadableDatabase(); | |
} | |
if(!mDatabase.isReadOnly()) { | |
mDatabase.close(); | |
mDatabase = getReadableDatabase(); | |
} | |
} | |
Cursor cursor = mDatabase.rawQuery("select DISTINCT tbl_name from sqlite_master where tbl_name = '"+tableName+"'", null); | |
if(cursor!=null) { | |
if(cursor.getCount()>0) { | |
cursor.close(); | |
return true; | |
} | |
cursor.close(); | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment