How to overwrite SQLite DB in iOS when a new version of an application is released

I have an iOS application that uses SQLite DB to store its data model. The user cannot modify the contents of this database in any way. The only thing that will change the contents of the database is when I add more content to future application updates. (The application never writes content to the database, only reads it)

So, when I perform such an update, I want him to use the β€œnew” version of the database in the new version that I am creating and just get rid of the old database. What is the best way to do this? Is there an easy way to just tell him to grab a new version of the database during the upgrade, or do I need to program in the user logic for this?

+6
source share
3 answers

Discuss from the very beginning your application was installed for the first time. Sqlite db will be initialized in the current steps:

  • Check if [appname] .db exists in the application installation folder.

  • Create an empty [appname] .db file in the installation folder if this file does not exist.

  • Create an entry to indicate the current version of the application and save it in a table called "appversion" (this can also be saved in the preferences file).

  • Download the old version number from the place where you stored it in step 3, compare it with the current version number, and you need to execute serval sql files with the name "[appname] _sql_v1.sql, [appname] _sql_v2.sql, [appname] _sql_v3 .sql ". Keep in mind that each edition will have a '[appname] _sql_v * .sql', which contains all table schema changes and records the changes. If your application has been upgraded to version 7, you will find 7 sql files in your application package.

  • Suppose some guy installed the application with version 2, but did not update until he finds the current version 7, and then he updates it to 7. Two code version numbers will be loaded when the application is first launched after the update is completed: 2 and 7, so these sql files: [appname] _sql_v3.sql, [appname] _sql_v4.sql, [appname] _sql_v5.sql, [appname] _sql_v6.sql, [appname] _sql_v7.sql will be executed one after another.

Remember this:

1. Do not put the init sql statement in the code, put it in the sql file and read it in sqlite db if necessary.

2. Each release will have a file [appname] _sql_v * .sql containing the changes made to the simple version.

+4
source

If you use your database from your package (I assume that you do, since your database is read-only), you automatically use the new version of the database to update the application. If you copy your database from the package to the documentation folder, simply rewrite the old database with the new one.

0
source

Add the preloaded database to your project (in the "Resources" folder). This will be deployed with your application and can be found at runtime in the main package path.

Since the user will never write content to it, but only read from it, you can open it directly from there. You can get the path for it using:

NSString *databasePath = [[NSBundle mainBundle] pathForResource:@"ReadOnlyDatabase" ofType:@"sqlite"]; 
0
source

Source: https://habr.com/ru/post/890576/


All Articles