Query If Android Database Exists!

Query if Android database exists!

/**
* Check if the database exist and can be read.
*
* @return true if it exists and can be read, false if it doesn't
*/
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
checkDB = SQLiteDatabase.openDatabase(DB_FULL_PATH, null,
SQLiteDatabase.OPEN_READONLY);
checkDB.close();
} catch (SQLiteException e) {
// database doesn't exist yet.
}
return checkDB != null;
}

where DB_FULL_PATH is the path to your database file.

And the reason I am not just checking if a file exists is because it would not tell whether (a) it's an sqlite db file, (b) the file is not corrupt and can actually be read, i.e. due to partial download or however it has been created.

How to check if a DB exists in Android?

when you create database it call when application start that time their db create.you used to below code in app activity and that activity call in manifest file in application class call like used below code ..

public class AppActivity extends Application {

AppDatabase db;

@Override
public void onCreate() {
super.onCreate();
db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "database-name").build();
}

public AppDatabase getDatabase() {
return db;
}

}
and add below line manifest file .. add below line in application tag

        android:name="AppActivity"

Check if database exists in sqlite

Option 1

Use onCreate to create multiple tables :-

@Override
public void onCreate(SQLiteDatabase database) {
String createDicaTable = "CREATE TABLE " + TABLE_NAME + "( " +
ID_DICA + " INTEGER UNSIGNED PRIMARY KEY," +
CHANNEL_DICA + " TEXT NOT NULL," +
TITULO_DICA + " TEXT NOT NULL," +
CONTEUDO_DICA + " TEXT NOT NULL" + ")";
database.execSQL(createDicaTable);

// Next table
String createanothertable = "CREATE TABLE anothertable (column TEXT)";
database.execSQK(createanothertable);
// etc
}

Note! onCreate only runs automatically when Database doesn't exist



Option 2

Create subsequent(missing) tables (assumes independence from other other tables) without having to remove existing tables.

Run as required or everytime App is run (hence code IF NOT EXISTS).

public void addMissingTables() {
String createanothertable = "CREATE TABLE IF NOT EXISTS anothertable (column TEXT)";
database.execSQL(createanothertable);
// etc
}
  • Note! assumes method is added to Databasehelper (so db)

  • Note! Shouldn't be a problem is used after instance of DatabaseHelper is obtained, as this.database = getWritableDatabase(); will force DatabaseHelper to create database if it doesn't exist.

  • You could also code IF NOT EXISTS for tables in onCreate and call it in a similar fashion.

Android - Does SQLite Database Exist?

Open your database and catch the SQLitException which will be thrown in case if database doesn't exist. Note that, you should not call the openOrCreateDatabase() method. See this post for details; Query if Android database exists!

How to check whether a Room Database exists?

Is there any way to check whether Room Database has been initialized?

If the room database has been built/opened (initialised) then it will contain a table called room_master_table.

Therefore, you could, if the open is successful, run a query to see if that table exists.

e.g.

Cursor csr = db.query("sqlite_master",null,"name = ?",new String[]{"room_master_table"},null,null,null);
if (csr.getCount() > 0) {
//db is a room database
} else {
// db is not a room database
}
csr.close();
  • Note the above is in-principle code, it has not been tested or run and may therefore contain some errors.

which always return false.

I suspect the issue is that your are appending .db to the database name. The .db extension is not necessary. If the database name is used without the extension when opening the database(building it in room) then the file name will not have the extension.

You would likely not have issues if you always just use DATABASE_NAME so :-

final String DB_PATH = context.getDatabasePath(DATABASE_NAME).getAbsolutePath();

Android SQL: Check if Record in Database Exists

if (cursor.moveToFirst()) {
// record exists
} else {
// record not found
}

Android - Check if a row within the DB exists

This is the most efficient way to find if a row exists or not.

SELECT EXISTS(SELECT 1 FROM playerTable WHERE playerNumber="Here comes the player number");

It will return 1 if there is a row with playerNumber in the table or will return 0 if there is no row in the table.

How to check if a Sqlite database exists in android?

It looks like OrmLiteSqliteOpenHelper inherits the getWritableDatabase() method from SQLiteOpenHelper. If that method returns null, then the database does not exist.



Related Topics



Leave a reply



Submit