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);
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);
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);
checkDB.close();
} catch (SQLiteException e) {
Log.e("Error", "No existe la base de datos " + .getMessage());
}
return checkDB != null;
}
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;
}
cursor.close();
}
return isExist;
}