一个Android帮助程序类,用于使用应用的raw asset来管理数据库创建和版本管理

网友投稿 696 2022-11-05

一个Android帮助程序类,用于使用应用的raw asset来管理数据库创建和版本管理

一个Android帮助程序类,用于使用应用的raw asset来管理数据库创建和版本管理

THIS PROJECT IS NO LONGER MAINTAINED

Android SQLiteAssetHelper

An Android helper class to manage database creation and version management using an application's raw asset files.

This class provides developers with a simple way to ship their Android app with an existing SQLite database (which may be pre-populated with data) and to manage its initial creation and any upgrades required with subsequent version releases.

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.

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.

Setup

Gradle

If you are using the Gradle build system, simply add the following dependency in your build.gradle file:

dependencies { compile 'com.readystatesoftware.sqliteasset:sqliteassethelper:+'}

Ant/Eclipse

If you are using the old build system, download the latest library JAR and put it in your project's libs folder.

Usage

SQLiteAssetHelper is intended as a drop in alternative for the framework's SQLiteOpenHelper. Please familiarize yourself with the behaviour and lifecycle of that class.

Extend SQLiteAssetHelper as you would normally do SQLiteOpenHelper, providing the constructor with a database name and version number:

public class MyDatabase extends SQLiteAssetHelper { private static final String DATABASE_NAME = "northwind.db"; private static final int DATABASE_VERSION = 1; public MyDatabase(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); }}

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:

A databases folder inside assetsA SQLite database inside the databases folder whose file name matches the database name you provide in code (including the file extension, if any)

For the example above, the project would contain the following:

assets/databases/northwind.db

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:

ZIP: assets/databases/northwind.db.zip (a single SQLite database file must be the only file within the archive)GZIP: assets/databases/northwind.db.gz

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.

super(context, DATABASE_NAME, context.getExternalFilesDir(null).getAbsolutePath(), null, DATABASE_VERSION);

The database is made available for use the first time either getReadableDatabase() or getWritableDatabase() is called.

The class will throw a SQLiteAssetHelperException if you do not provide the appropriately named file.

The SQLiteOpenHelper methods onConfigure, onCreate and onDowngrade are not supported by this implementation and have been declared final.

The samples:database-v1 project demonstrates a simple database creation and usage example using the classic Northwind database.

Database Upgrades

At a certain point in your application's lifecycle you will need to alter it's database structure to support additional features. You must ensure users who have installed your app prior to this can safely upgrade their local databases without the loss of any locally held data.

To facilitate a database upgrade, increment the version number that you pass to your SQLiteAssetHelper constructor:

private static final int DATABASE_VERSION = 2;

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:

assets/databases/_upgrade_-.sql

For example, 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.

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.

The samples:database-v2-upgrade project demonstrates a simple upgrade to the Northwind database which adds a FullName column to the Employee table.

Generating upgrade scripts

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 for Windows.

Upgrades via overwrite

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.

You can additionally pass an argument that is the version number below which the upgrade will be forced.

Note that this will overwrite an existing local database and all data within it.

Credits

####Author:

Jeff Gilfelt

Contributors:

Alexandros SchillingsCyril MottierJon AdamsKevin

License

Copyright (C) 2011 readyState Software LtdCopyright (C) 2007 The Android Open Source ProjectLicensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License at http://apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:字节流中的 read() 方法解析
下一篇:字符流实现文本文件的拷贝处理
相关文章

 发表评论

暂时没有评论,来抢沙发吧~