Use sqlite database from Android app resources

☆樱花仙子☆ 提交于 2020-01-02 10:25:54

问题


I've started Android programming recently so bear with me :)

I develop an app which has all of its content stored in database.sql file. I've implemented a subclass of SQLiteOpenHelper and added database.sql to my project assets.

public class DBHelper extends SQLiteOpenHelper
{
public DBHelper (Context ctx)
{
  super(ctx, "database.sql", null, 1);
}
}

This thing doesn't work. I just get an exception every time I try to do smth with DB :(


回答1:


You need to override onCreate and onUpdate as well, have a look to google's example here




回答2:


You have 3 choices when you want to create a database locally in Android; hopefully they will support deployment directly from the APK soon. The database needs to be in a specific location :

/data/data/YOUR_PACKAGE/databases/

  1. you can download the database and write it to the database folder from a known Net location.
  2. you can create the database using code.
  3. you can copy the database from your assets folder to the database folder (doubles space required).

Also see http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/



来源:https://stackoverflow.com/questions/1894984/use-sqlite-database-from-android-app-resources

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!