Skip to content

Instantly share code, notes, and snippets.

@nxax
Created August 5, 2012 18:56
Show Gist options
  • Select an option

  • Save nxax/3266683 to your computer and use it in GitHub Desktop.

Select an option

Save nxax/3266683 to your computer and use it in GitHub Desktop.
Check table if exists on SQLite
// 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;
}
@nurzaly

nurzaly commented Feb 2, 2016

Copy link
Copy Markdown

hi what is mDatabase declaration?

can you show me how to call this method..

thanks

@vijayakumar52

Copy link
Copy Markdown

Good work

@neri4488

Copy link
Copy Markdown

hi what is mDatabase declaration?

can you show me how to call this method..

thanks

@Nuno99Santos

Copy link
Copy Markdown

Hi all.

mDatabase is the connection to the table database.

Regards

@meldouh

meldouh commented Jul 12, 2018

Copy link
Copy Markdown

hi what is getReadableDatabase();
thanks.

@wjkhappy14

Copy link
Copy Markdown

application/x-sqlite3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment