简体   繁体   中英

How should I insert permanent data using SQLite database in android studio?

I want to make a travel guide app using android studio. I want to insert some data about travel destinations such as: their name, location, and etc. I think that data is permanent, not a dynamic one. How should I put the code for inserting that data? Do I have to make one class that contain the code for inserting all the data to SQLite database?

What you want is called propulate a database .

The current approach from the android team is to use Room to handle database actions. You can create a sqlite database locally, insert some data and add it to your andorid project's assets folder (you might need to create it first). Then at the first start of your app, you can copy the content to the database of your app by using Room's createFromAsset() method.

To prepopulate a Room database from a prepackaged database file that is located anywhere in your app's assets/ directory, call the createFromAsset() method from your RoomDatabase.Builder object before calling build():

Room.databaseBuilder(appContext, AppDatabase.class, "Sample.db")
    .createFromAsset("database/myapp.db")
    .build();

The createFromAsset() method accepts a string argument that contains a relative path from the assets/ directory to the prepackaged database file. (Source: Prepopulate your Room database )

Check out the guide here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM