In my android application I am inserting and displaying data from a SQLite database. Before that I want to check if the database is available or not. That is, I want to check whether the table is available. If it is available, I also want to check whether it is not empty. Here is the pseudocode
if (DB available) {
if (table available) {
if (table != empty) {
insertdata()
}
}
}
Generally in Android it’s not necessary to check explicitly if your database is available. The flow is that you try to get a database instance (in read or write mode) and if the database is not existing, an onCreate() procedure is executed, which should execute DDL statement and create database for the first time. In your DDL statement, you can use:
which will guarantee you that TABLE_NAME exists before you query it.
You can use
getWritableDatabase()called onSQLiteOpenHelperObject. It will throw SQLiteException if there’s problem with the database.Check out the official documentation for more information.