简体   繁体   English

从Android中的外部存储访问或打开数据库

[英]Access or open database from external storage in android

--> Okay , here i have a Sq-lite database in SD card. ->好的,这里我在SD卡中有一个Sq-lite数据库。

  (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. 让我知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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