2

i want to check if the database existed, and i have found some code from internet.

     private boolean checkDataBase(){

        SQLiteDatabase checkDB = null;

        try{
            String myPath = DB_PATH + DB_NAME;
            checkDB = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READONLY);

        }catch(SQLiteException e){

            //database does't exist yet.

        }

        if(checkDB != null){

            checkDB.close();

        }

        return checkDB != null ? true : false;
    }

but i got an error..cannot open file why it can't work?

: sqlite returned: error code = 14, msg = cannot open file at line 27703 of [8609a15dfa], db=/data/data/com.testing.abcde/databases/scj
: sqlite returned: error code = 14, msg = os_unix.c: open() at line 27703 - "" errno=2 path=/data/data/com.testing.abcde/databases/scj, db=/data/data/com.testing.abcde/databases/scj
: sqlite3_open_v2("/data/data/com.testing.abcde/databases/scj", &handle, 1, NULL) failed
: Failed to open the database. closing it.
: android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file
:   at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
:   at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1013)
:   at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:986)
:   at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:962)
:   at com.lkland.softkeyboard.easytype.SQLite.checkDataBase(SQLite.java:80)
:   at com.lkland.softkeyboard.easytype.SQLite.createDataBase(SQLite.java:55)
LK Yeung
  • 3,462
  • 5
  • 26
  • 39
  • You should accept some answers on previos questions in order to get more new answers. – nkr Jul 25 '12 at 19:41
  • possible duplicate of [Query if Android database exists!](http://stackoverflow.com/questions/3386667/query-if-android-database-exists) – rds May 09 '15 at 10:55

1 Answers1

3

I use this code, which seems to work well:

// Where DB_PATH is /data/data/com.testing.abcde/databases/scj
// and DB_NAME is name.db
public boolean checkDBExists() {
     File dbFile = new File(DB_PATH + DB_NAME);
     return dbFile.exists();
}

You will need to import java.io.File; or provide a full reference to the File class.

Tushar
  • 8,019
  • 31
  • 38