Verify BD existence and table on Android


How can I verify the existence of a table in the bd sqlite in an Android app?

  SQLiteDatabase database = SQLiteDatabase.openDatabase(ruta_base_de_datos, null, SQLiteDatabase.OPEN_READONLY);
asked by Eduardo 02.05.2017 в 15:59

1 answer


How to know if the database exists?.

For this you need to know the path where you made the creation of the database, and use this method, which attempts the connection to determine if it exists in reality:

private boolean checkDataBase(String Database_path) {
    SQLiteDatabase checkDB = null;
    try {
        checkDB = SQLiteDatabase.openDatabase(Database_path, null, SQLiteDatabase.OPEN_READONLY);
    } catch (SQLiteException e) {
        Log.e("Error", "No existe la base de datos " + .getMessage());
    return checkDB != null;

How to know if the table in a SQlite database exists?

For that you can use this method which looks for the record of the table inside the table sqlite_master and checks if it exists by means of the name:

public boolean isTableExists(String nombreTabla) {
    boolean isExist = false;
    Cursor cursor = db.rawQuery("select DISTINCT tbl_name from sqlite_master where tbl_name = '" + nombreTabla + "'", null);
    if (cursor != null) {
        if (cursor.getCount() > 0) {
            isExist = true;
    return isExist;
answered by 02.05.2017 / 16:21