我可以在 /sdcard 上下载 SQLite 数据库并从我的 Android 应用程序访问它吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1055188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:39:02  来源:igfitidea点击:

Can I download an SQLite db on /sdcard and access it from my Android app?

databaseandroidsqlitedownload

提问by kzyapkov

I already found out that there is no way to bundle files in an .apk and have them on /sdcard, the best option so far being to download the large files upon first run. I came accross a tutorial saying how to bundle an sqlite db with the apk and then copy it so that it can be accessed with SQLiteDatabase (thus doubling the space needed, and not using /sdcard at all).

我已经发现无法将文件捆绑在 .apk 中并将它们放在 /sdcard 上,目前最好的选择是在第一次运行时下载大文件。我遇到了一个教程,说如何将一个 sqlite db 与 apk 捆绑在一起,然后复制它,以便可以使用 SQLiteDatabase 访问它(从而将所需的空间加倍,并且根本不使用 /sdcard)。

http://developer.android.com/guide/topics/data/data-storage.html#dbsays all databases MUST be in /data/data/package_name/databases.

http://developer.android.com/guide/topics/data/data-storage.html#db说所有数据库都必须在 /data/data/package_name/databases 中。

Is that really so? Is there a way to trick the framework into opening a database that is placed on the /sdcard partition? Is there a way to use another SQLite java wrapper/framework to access such databases?

真的是这样吗?有没有办法欺骗框架打开放置在 /sdcard 分区上的数据库?有没有办法使用另一个 SQLite java 包装器/框架来访问这样的数据库?

If the answer to the above is 'No', what other options do I have? My data is very well represented in a relational model, but is just too big, plus, I want to be able to update it without the need to reinstall/upgrade the entire app.

如果以上问题的答案是“否”,我还有什么其他选择?我的数据在关系模型中得到了很好的表示,但太大了,另外,我希望能够更新它而无需重新安装/升级整个应用程序。

回答by AdamC

Sure you can. The docs are a little conflicting about this as they also say that no limitations are imposed. I think they should say that relative paths are to the above location and dbs there ARE private. Here is the code you want:

你当然可以。文档对此有点矛盾,因为他们还说没有施加任何限制。我认为他们应该说相对路径是到上述位置,而 dbs 是私有的。这是你想要的代码:

File dbfile = new File("/sdcard/mydb.sqlite" ); 
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
System.out.println("Its open? "  + db.isOpen());

回答by Iwan

Try this:

尝试这个:

String dir = Environment.getExternalStorageDirectory().getPath()
File dbfile = new File(dir+"/mydb.sqlite"); 
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
System.out.println("Its open? "  + db.isOpen());

回答by Sergio

I share next code. Declare opcionesMenu Vector<String>

我分享下一个代码。宣布opcionesMenu Vector<String>

Vector < String > opcionesMenu = new Vector< String >();
// the database is SDCard. I saw  the code to Blackberry (net.rim)
String rutaDB = "/storage/extSdCard/Android/databases/Offshore.db";


    SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(rutaDB, null);

    Cursor cursor = db.rawQuery("SELECT Codigo, Nombre FROM Ruta ORDER BY Codigo, Nombre", null);

    if (cursor.moveToFirst())
    {
        do
        {
            opcionesMenu.add(cursor.getString(0) + " - " + cursor.getString(1));
        } while(cursor.moveToNext());
    }

    db.close();

回答by Barna

Just an idea:

只是一个想法:

  • you can put your database.db into the assets folder.

  • if your database.db file is larger than 2Mb the system is unable to compress it, so you need other one options

  • you can rename your database.db for example database.jit or database.mp3 - which are not compressed, than at the first run you can rename it to database.db

  • 您可以将 database.db 放入资产文件夹中。

  • 如果您的 database.db 文件大于 2Mb,系统将无法压缩它,因此您需要其他一种选择

  • 你可以重命名你的 database.db 例如 database.jit 或 database.mp3 - 它们没有被压缩,而不是在第一次运行时你可以将它重命名为 database.db