![]() ![]() ( Note: You might want to run the database code in a separate thread if it uses a lot of resources.) package android.example Here is an activity that accesses the database and then uses it. Here is an entry to add to the /res/values/strings.xml file for the database version number. INSERT INTO "picnic_table" VALUES ('paper') DROP TABLE "picnic_table" Uncomment this if picnic table was previously created and now is being replaced.ĬREATE TABLE "picnic_table" ("plates" TEXT) Public class DatabaseOpenHelper extends SQLiteOpenHelper Then refresh the folder to verify the copy processĬreate a subclass for SQLiteOpenHelper, with connect, close, path, etc. While ((length = inputStream.read(buffer))>0) OutputStream outputStream = new FileOutputStream(dbFilePath) InputStream inputStream = context.getAssets().open("testDB.db") String appDataPath = context.getApplicationInfo().dataDir įile dbFolder = new File(appDataPath + "/databases") //Make sure the /databases folder existsĭbFolder.mkdir() //This can be called multiple times.įile dbFilePath = new File(appDataPath + "/databases/testDB.db") call this if API level is lower than 17 String appDataPath = "/data/data/" + context.getPackageName() + "/databases/" get context by calling "this" in activity or getActivity() in fragment It can also be used to update(by overwriting) the database file in future. Use the following code to do the copy paste. Now, the copy file process is pretty much like the what Java is doing. And by default, the database file will be place in another folder called databases under this directory ![]() This is the app's default data folder mentioned above. The database file needs to be copied to /databases folder. ![]() Note that during app update, this database file will not be changed in the app's data folder. This overwrite process is useful when you want to update the database in future during the app update. If you call this code multiple times, the database file in data folder will be overwritten by the one in assets folder. This is a one time action (initialization) to copy the database file. You need to copy the database file to the app's data folder in order to do further interaction with it. Step 3: Copy the file to the app's data folder Then copy and paste the database file into this folder Step 2: Import the file into your projectĬreate the assets folder if you haven't had one. It has one table and some sample data in it like this In this example, I prepared a file called testDB.db. sqlite file, all you need to do is to change file extension names. The advantage is that you don't need to do a complex initialization, which sometimes costs lots of time, if your data set is huge. Shipping the app with a database file is a good idea for me. Shipping the app with a database file, in Android Studio 3.0 ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |