You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.markdown
+21-13Lines changed: 21 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ This class provides developers with a simple way to ship their Android app with
7
7
8
8
It is implemented as an extension to `SQLiteOpenHelper`, providing an efficient way for `ContentProvider` implementations to defer opening and upgrading the database until first use.
9
9
10
-
Rather than implementing `onCreate()` and `onUpgrade()` methods to execute a bunch of SQL statements, developers simply include appropriately named file assets in their project's `assets` directory. These will include the initial SQLite database file for creation and optionally any SQL upgrade scripts.
10
+
Rather than implementing the `onCreate()` and `onUpgrade()` methods to execute a bunch of SQL statements, developers simply include appropriately named file assets in their project's `assets` directory. These will include the initial SQLite database file for creation and optionally any SQL upgrade scripts.
11
11
12
12
Setup
13
13
-----
@@ -45,19 +45,19 @@ public class MyDatabase extends SQLiteAssetHelper {
45
45
}
46
46
```
47
47
48
-
SQLiteAssetHelper relies upon asset file and folder naming conventions. At minimum, you must provide the following in your `assets`directory, which will either be in your project root directory or under `src/main`in the gradle project structure:
48
+
SQLiteAssetHelper relies upon asset file and folder naming conventions. Your `assets`folder will either be under your project root, or under `src/main`if you are using the default gradle project structure. At minimum, you must provide the following:
49
49
50
-
* A `databases`subdirectory inside `assets`
51
-
* A SQLite database inside the `databases`subdirectory whose file name matches the database name you provide in code (including the file extension, if any)
50
+
* A `databases`folder inside `assets`
51
+
* A SQLite database inside the `databases`folder whose file name matches the database name you provide in code (including the file extension, if any)
52
52
53
-
For the example above, ther project would contain the following:
53
+
For the example above, the project would contain the following:
54
54
55
-
`src/main/assets/databases/northwind.db`
55
+
assets/databases/northwind.db
56
56
57
-
Earlier versions of this library required the database asset to be compressed within a ZIP archive. This is no longer required, but is still supported. Applications still targeting Gingerbread or lower should continue to provide a compressed archive to ensure large database files are not corrupted during the packaging process. The more Linux friendly GZIP format is also supported. The naming conventions using the above example are as follows:
57
+
Earlier versions of this library required the database asset to be compressed within a ZIP archive. This is no longer a requirement, but is still supported. Applications still targeting Gingerbread (API 10) or lower should continue to provide a compressed archive to ensure large database files are not corrupted during the packaging process. The more Linux friendly GZIP format is also supported. The naming conventions using the above example are as follows:
58
58
59
-
* ZIP: `src/main/assets/databases/northwind.db.zip` (a single SQLite database file must be the only file within the archive)
* ZIP: `assets/databases/northwind.db.zip` (a single SQLite database file must be the only file within the archive)
60
+
* GZIP: `assets/databases/northwind.db.gz`
61
61
62
62
The database will be extracted from the assets and copied into place within your application's private data directory. If you prefer to store the database file somewhere else (such as external storage) you can use the alternate constructor to specify a storage path. You must ensure that this path is available and writable whenever your application needs to access the database.
63
63
@@ -67,6 +67,8 @@ The database is made available for use the first time either `getReadableDatabas
67
67
68
68
The class will throw a `SQLiteAssetHelperException` if you do not provide the appropriately named file.
69
69
70
+
The SQLiteOpenHelper methods `onConfigure`, `onCreate` and `onDowngrade` are not supported by this implementation and have been declared `final`.
71
+
70
72
The [samples:database-v1](https://github.com/jgilfelt/android-sqlite-asset-helper/tree/v2/samples/database-v1) project demonstrates a simple database creation and usage example using the classic Northwind database.
71
73
72
74
Database Upgrades
@@ -76,13 +78,15 @@ At a certain point in your application's lifecycle you will need to alter it's d
76
78
77
79
To facilitate a database upgrade, increment the version number that you pass to your `SQLiteAssetHelper` constructor:
78
80
79
-
private static final int DATABASE_VERSION = 2;
81
+
```java
82
+
privatestaticfinalintDATABASE_VERSION=2;
83
+
```
80
84
81
85
Update the initial SQLite database in the project's `assets/databases` directory with the changes and create a text file containing all required SQL commands to upgrade the database from its previous version to it's current version and place it in the same folder. The required naming convention for this upgrade file is as follows:
For example, [northwind.db_upgrade_1-2.sql](https://github.com/jgilfelt/android-sqlite-asset-helper/blob/v2/samples/database-v2-upgrade/src/main/assets/databases/northwind.db_upgrade_1-2.sql) upgrades the database named "northwind" from version 1 to 2. You can include multiple upgrade files to upgrade between any two given versions.
89
+
For example, [northwind.db_upgrade_1-2.sql](https://github.com/jgilfelt/android-sqlite-asset-helper/blob/v2/samples/database-v2-upgrade/src/main/assets/databases/northwind.db_upgrade_1-2.sql) upgrades the database named "northwind.db" from version 1 to 2. You can include multiple upgrade files to upgrade between any two given versions.
86
90
87
91
If there are no files to form an upgrade path from a previously installed version to the current one, the class will throw a `SQLiteAssetHelperException`.
88
92
@@ -92,9 +96,13 @@ The [samples:database-v2-upgrade](https://github.com/jgilfelt/android-sqlite-ass
92
96
93
97
You can use 3rd party tools to automatically generate the SQL required to modify a database from one schema version to another. One such application is [SQLite Compare Utility](http://www.codeproject.com/KB/database/SQLiteCompareUtility.aspx) for Windows.
94
98
95
-
### Forcing upgrades
99
+
### Upgrades via overwrite
100
+
101
+
If you have a read-only database or do not care about user data loss, you can force users onto the latest version of the SQLite database each time the version number is incremented (overwriting the local database with the one in the assets) by calling the `setForcedUpgrade()` method in your `SQLiteAsstHelper` subclass constructor.
102
+
103
+
You can additionally pass an argument that is the version number below which the upgrade will be forced.
96
104
97
-
You can force users onto the latest version of the SQLite database (overwriting the local database with the one in the assets) by calling the `setForcedUpgradeVersion(int version)` method in your constructor. The argument passed is the version number below which the upgrade will be forced. Note that this will forcibly overwriting any existing local database and all data within it.
105
+
Note that this will overwrite an existing local database and all data within it.
0 commit comments