简体   繁体   中英

Access or open database from external storage in android

--> Okay , here i have a Sq-lite database in SD card.

  (String DB_PATH = Environment.getExternalStorageDirectory().getPath().toString();)

--> Checking if database exists

        dbFile = new File(DB_PATH + DB_NAME );
        if(dbFile.exists()== true)
        {
            //set text db exists
            open_DB();

        }
        else 
        {
            //set text db does not exist
            return;
        }

--> on debugging i could confirm the existence of the database file. but when opening the
database i get exception

    SQLiteDatabase checkDB = null;
    try 
    {
        dbFile = new File(DB_PATH + DB_NAME);
        myDataBase = SQLiteDatabase.openOrCreateDatabase(dbFile, null);

        //myDataBase = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, 0);
        //also tried this

        //set text bd opened
    }
    catch(SQLiteException e) 
    {
        //set text bd unable open
        return;
    }

Probably your problem is caused by flags or file so try to use this:

dbFile = new File(DB_PATH + DB_NAME);
if (dbFile.exists()) {
   db = SQLiteDatabase.openDatabase(db_path, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.CREATE_IF_NECESSARY);
}

Let me know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM