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 inonCreate
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
How to Implement a Contentobserver for Call Logs
Android Communication Between Two Applications
How to Get the Number of Unread Gmail Mails (On Android)
How to Enable Standard Copy Paste for a Textview in Android
Android Detect Done Key Press for Onscreen Keyboard
Which Android Ide Is Better - Android Studio or Eclipse
Adding Local .Aar Files to My Gradle Build
Exception:Aapt2 Error: Check Logs for Details
How to Set Dropdown Arrow in Spinner
Finish Parent and Current Activity in Android
How to Cancel an Dialog Themed Like Activity When Touched Outside the Window
How to Get Multiple Icons to Launch Different Activities in One Application
Android: Tabhost Without Tabactivity
Getting the Dimensions of the Soft Keyboard
Convert Bitmap Array to Yuv (Ycbcr Nv21)
Android Displaymetrics Returns Incorrect Screen Size in Pixels on Ics
Add a Search Filter on Recyclerview with Cards
How to Repeat a Method Every 10 Minutes After a Button Press and End It on Another Button Press