How to check if there is any sqlite database in the application?

1

I have the following dilemma:

I am putting together an app where I perform a synchronization between a MySqle database and SQLite using a Webservice.

My database is already available in MySql, and in my app during my splash screen I need to do a check: If there is any database already synchronized in the system the user is redirected to the login screen. If not, it should be redirected to the screen where it must inform the data to perform the synchronization.

If it was a check in a table I would have to inform the database name to my SQLiteOpenHelper and thus perform the operation on a certain table, but how do I do this in the database itself?

    
asked by anonymous 09.01.2016 / 21:57

2 answers

3

You can check by passing the specific name of a database. Something like this:

public static boolean doesDatabaseExist(ContextWrapper context, String dbName) {
    File dbFile = context.getDatabasePath(dbName);
    return dbFile.exists();
}

EDIT:

Complementing as suggested:

If on Android you have never called the method where you create the table (and therefore the file) this method will return false (since it does not exist yet), otherwise it will return true.

It can also be in any class. In my projects it usually stays in a DbHelper class or DbUtils, along with other database utilities.

    
09.01.2016 / 22:50
0

I decided to create a default database containing the name and id of the user, whenever the application is opened I check in this database if it is populated - > Login screen, if not - > Synchronization Screen, where the user informs his data, after synchronization the default database receives the name and id, and a new database with the user's token is created.

    
11.01.2016 / 13:36