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 }
source share