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

Asked

Viewed 1,877 times

2

I have the following dilemma:

I’m setting up 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 make 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 should inform the data to perform synchronization.

If it was a check in a table I would have to enter the name of the database for my Sqliteopenhelper and so perform the operation on a given table, but how do I do this in the database itself?

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 creates the table (and therefore the file) this method will return false (because there is no such BD yet), otherwise it will return true.

He can also be in any class. In my projects it usually stays in a Dbhelper or Dbutils class, along with other database utility methods.

  • But does this check if the database(file .sqlite) exists? Type, if the application is starting for the first time?

  • If you have never called the method where creates the table (and therefore the file) this method will return false (because there is no such BD yet), otherwise it will return true.

  • The best solution would be to show the login screen with an option below written sign up.

  • So this method should be added to the auxiliary class of the bank (which extends Sqliteopenhelper) right? P.S.: I’m not the one who asked, just curious, even hehe

  • 1

    Yes, only after I saw that it was not the same of the question. Haha. This method can be in any class. In my projects it usually stays in a Dbhelper or Dbutils class, along with other database utility methods.

  • 1

    +1 for the clarification. Just a hint, it would be interesting to add these clarifications in the answer too, so it gets even clearer and complete, and more passive to be positive by other colleagues :)

  • Okay, obg. Done! :)

  • Great answer Luiz, but in my case it won’t work. Let me explain: In my case, the name of each database would be defined by a different token related to each company registered in the app, so before performing a first synchronization I would not have to inform the name of the database to use with your method, but still thank you very much for the reply.

Show 3 more comments

0

I decided to create a standard 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 gets the name and id, and a new database with the user token is created.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.