android如何访问assets文件夹中包含的sqlite数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2318784/
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 05:24:11  来源:igfitidea点击:

How does android access a sqlite database included in the assets folder

androidsqlite

提问by user275788

I already have a SQLite database. I put it in the assetsfolder of my project. I read the Android documentation. It said that for all the databases in Android, the path is data/data/pack_name/database_name.

我已经有一个 SQLite 数据库。我把它放在assets我的项目文件夹中。我阅读了 Android 文档。它说对于Android中的所有数据库,路径都是data/data/pack_name/database_name.

This confused me. I just placed it in the assetsfolder, so the path is data/data/assets/database_name?

这让我很困惑。我只是把它放在assets文件夹中,所以路径是data/data/assets/database_name

回答by ccheneson

The package name is not the project name, the package name is the namespace. From Anthony's link.

包名不是项目名,包名是命名空间。来自安东尼的链接。

Remember to change the "YOUR_PACKAGE" to your application package namespace (i.e: com.examplename.myapp) in the DB_PATH string.

请记住将 DB_PATH 字符串中的“YOUR_PACKAGE”更改为您的应用程序包命名空间(即:com.examplename.myapp)。

For example, from the Hello World tutorial, the project name is HelloAndroidbut the package name is com.example.helloandroid

例如,从Hello World 教程中,项目名称是HelloAndroid但包名称是com.example.helloandroid

If this application had a database, it would be stored at data/data/com.example.helloandroid/database

如果此应用程序有一个数据库,它将存储在 data/data/com.example.helloandroid/database

To see how it is for the other applications you can start your emulator. On the menu bar you have your avd's name (I think it stands for Android Virtual Device). On mine it s "avdessay:5554"

要查看其他应用程序的情况,您可以启动模拟器。在菜单栏上有您的 avd 名称(我认为它代表 Android Virtual Device)。在我的是“avdessay:5554”

(On Linux) From command line, type:

(在 Linux 上)从命令行输入:

adb -s emulator-5554 shell

adb -s emulator-5554 shell

You have to replace 5554 by whatever port you are using.

您必须用您使用的任何端口替换 5554。

if you have the command prompt '#' you can type:

如果您有命令提示符“#”,您可以输入:

cd data/data

cd data/data

There, you will see that eveything is in a form of a package name.

在那里,您将看到所有内容都采用包名称的形式。

More info here

更多信息在这里

回答by Anthony Forloney

When you createa database by utilizing the SQLiteDatabaseor SQLiteOpenHelperclasses, it creates the database in your data/data/package_name/database.

当您创建利用数据库SQLiteDatabase或者SQLiteOpenHelper类,它在你创建数据库data/data/package_name/database

You can access that resource by using

您可以通过使用访问该资源

InputStream myInput = myContext.getAssets().open(your_database_here);

Any other information, look at Using your own SQLite database in Android Applications

任何其他信息,请查看在 Android 应用程序中使用您自己的 SQLite 数据库

The package_nameportion of the path, would be the name of your package. You can find the name of the package at the first line in your .javafiles.

package_name路径的一部分将是您的的名称。您可以在.java文件的第一行找到包的名称。

As an example, my class starts with this at the top

例如,我的课程从顶部开始

package com.forloney.tracker;

So my database is in data/data/com.forloney.tracker/databasefolder.

所以我的数据库在data/data/com.forloney.tracker/database文件夹中。

Hope this makes sense.

希望这是有道理的。

回答by Dan

@ScCrow I too followed thisexample and had the same problems you did until I realized I was not using the DataBaseHelper correctly (or rather it had a quirk I overlooked).

@ScCrow 我也遵循了这个例子并且遇到了和你一样的问题,直到我意识到我没有正确使用 DataBaseHelper(或者它有一个我忽略的怪癖)。

When you use your DatabaseHelper class in your activity, you have to make sure you call createDatabase first! If you look at the code for openDatabase it does NOT check to see if the database exists, so you either have to (attempt to) create the database in each activity you use it in, or modify the openDatabase method to check to make sure the db exists. The link posted does actually instruct you to use it this way but you (like me) may have glossed over that.

当您在活动中使用 DatabaseHelper 类时,您必须确保首先调用 createDatabase!如果您查看 openDatabase 的代码,它不会检查数据库是否存在,因此您必须(尝试)在使用它的每个活动中创建数据库,或者修改 openDatabase 方法以检查以确保db 存在。发布的链接确实指示您以这种方式使用它,但您(像我一样)可能已经掩盖了这一点。

Bad:

坏的:

DBAdapter db = new DBAdapter(this);
db.openDataBase(); //Bad! db not created yet!

Good:

好的:

DBAdapter db = new DBAdapter(this);
db.createDataBase(); //needs exception handling
db.openDataBase();

回答by user2343520

In your DBAdapter.java, change the return type of openDatabasemethod to SQLiteDatabase.

在您的 DBAdapter.java 中,将openDatabase方法的返回类型更改为SQLiteDatabase.

When you access the database simply use SQLiteDatabase data = db.openDatabase(), where dbis DBAdapter db = new DBAdapter(this).

当您访问数据库时,只需使用SQLiteDatabase data = db.openDatabase(), where dbis DBAdapter db = new DBAdapter(this)

回答by ScCrow

When I try to open my DB, I get "unable to open database file". I assume its not finding the DB and not some other programmer error. In the log I see the following which looks good to me.

当我尝试打开我的数据库时,出现“无法打开数据库文件”。我假设它没有找到数据库而不是其他一些程序员错误。在日志中,我看到以下内容对我来说很好。

   sqllite3_open_v2("/data/data/com.isildo.HelloListView/databases/ListsDB" ...

This is the setup

这是设置

private static String DB_PATH = "/data/data/com.isildo.HelloListView/databases/";    
private static String DB_NAME = "ListsDB";

In my projects assets in the Package Explorer, I see the ListsDb database.

在包资源管理器中我的项目资产中,我看到了 ListsDb 数据库。

So I at least think I have it all correct. I am using the example at [http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/]

所以我至少认为我所有的都是正确的。我正在使用 [ http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/] 中的示例

In one of the posts here, someone offers a suggestion about setting some Assets parameters. {no response to post there}

在此处的其中一篇文章中,有人提出了有关设置某些资产参数的建议。{没有回复在那里发帖}

**To get an ASSETS folder into your APK:
  In /nbproject/project.properties, 
     change assets.dir=
to
     assets.dir=assets 
     assets.available=true
In /nbproject/build-impl.xml, there is line in the “if=assets.available” target that reads
that needs to be changed to**

Is this something we need to do, and if so, can we get a little better direction on the changes required. I could not find the places to make the suggested changes I looked at the project settings, and other things.

这是我们需要做的事情吗,如果是这样,我们能否在所需的更改上获得更好的指导。我无法找到进行建议更改的地方,我查看了项目设置和其他内容。

Yep, Im new to the environment, so I may just be not finding them. Im using Eclipse on windows.

是的,我对环境不熟悉,所以我可能找不到它们。我在 Windows 上使用 Eclipse。