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

后端 未结 5 1230
借酒劲吻你
借酒劲吻你 2020-12-18 07:31

I already have a SQLite database. I put it in the assets folder of my project. I read the Android documentation. It said that for all the databases in Android,

5条回答
  •  南方客
    南方客 (楼主)
    2020-12-18 07:36

    @ScCrow I too followed this example 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).

    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.

    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();
    

提交回复
热议问题