Android - does SQLite database exist?

How can I quickly check if a database exists in Android?

(and not a table - the entire database)

+6
source share
2 answers

Open your database and catch the SQLitException that will be thrown in case the database does not exist. Note that you should not call the openOrCreateDatabase() method. See This Post for details; Query if Android database exists!

+5
source

My way is to check if your database exists, open the file descriptor and see if the file exists. from my experience using the openOrCreateDatabase () function, an error will not be thrown if db does not exist. take a look at the name "Open OR Create Database". this means that if there is no db to open, it will just make a new one, so no matter if it was before, there is one. I think the only time you get an error message is that it cannot do this. which, from what the guy said that I do not believe, is what they are trying to check, I think he wanted his program to find out if the db was already there before, and not automatically create an empty one if it wasnโ€™t, therefore, since all db are stored as files, the first parameter in this open database command is the file name. Therefore, I believe the best option is to check if the db file exists like this:

 File dbtest = new File("/data/data/yourpackagename/databases/dbfilename"); //If you have Context, you could get the Database file using the following syntax //File dbtest = getApplicationContext().getDatabasePath("dbfilename.db"); if(dbtest.exists()) { // what to do if it does exist } else { // what to do if it doesn't exist } 
+2
source

Source: https://habr.com/ru/post/890976/


All Articles