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

Overview

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 assets
  • A 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/<database_name>_upgrade_<from_version>-<to_version>.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:

Contributors:

License

Copyright (C) 2011 readyState Software Ltd
Copyright (C) 2007 The Android Open Source Project

Licensed 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://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed 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 and
limitations under the License.
Comments
  • Added splitSqlScript function to parse SQL DB upgrade scripts

    Added splitSqlScript function to parse SQL DB upgrade scripts

    DB upgrade scripts that contain semicolons in strings for inserts / updates are not parsed correctly. e.g.

    1: CREATE TABLE temp (a TEXT); 2: INSERT INTO temp VALUES ("this;that");

    was previously parsed as:

    2: INSERT INTO temp VALUES ("this; 3: that");

    I have added a splitSqlScript function which I have taken from http://www.michenux.net/android-database-sqlite-creation-upgrade-245.html (search for splitSqlScript).

    Note: I have modified the code on Line 345 to check for a double quote character rather than the single quote character used in the linked page. This is to align with the example-v2 code which uses double quotes in SQL scripts.

    I have tested this for my project and it is correctly handling semicolons. I have also generated a new .jar but it has doubled the size of the .jar (12kb -> 23kb).

    opened by kevinchai 2
  • Fixed documentation bug.

    Fixed documentation bug.

    The documentation makes an incorrect statement here and I added some clarification. After tracing on the ADB (and reading through your code) I discovered that the name of the zip archive must have .db in it.

    opened by jsinglet 1
  • Fix incorrect alphanumeric sorting of upgrade scripts that can cause them to be applied in the wrong order

    Fix incorrect alphanumeric sorting of upgrade scripts that can cause them to be applied in the wrong order

    This patch fixes the case when applying many upgrade scripts at once that go up to the next digit, for example from 9 to 10, or 99 to 100, from being applied in the wrong order.

    The patch upgrades the Sort call on the file list to compare paths by their upgrade version numbers, instead of using alphanumeric comparison on plain file names.

    For example, when applying 2 different upgrade files from version 9 to 11, this fix keeps it from using the incorrect alphanumeric order of 10_11 before 9_10, and instead runs them in the correct order of 9_10 then 10_11.

    I thought about changing some of the logic order so that it can do this compare earlier without the regular expression parsing, but that would be a fairly decent logic adjustment. So I opted for keeping this code separated out into its own class (but still nested in the main class).

    opened by jon-adams 1
  • Allow/Disallow nested transactions

    Allow/Disallow nested transactions

    As I kept forgetting to remove the "BEGIN TRANSACTION" and "COMMIT TRANSACTION" lines of the migration script produced by the SQLite Compare Utility, I made the library's default behaviour to skip them.

    This will essentially treat any nested transactions in the migration script as part of the parent one.

    If someone really wants to, nested transactions can be re-enabled by calling "setAllowNestedTransactions(true)".

    (I told you I am bad at repetitive tasks)

    -Alex

    opened by alt236 1
  • don't skip .sql upgrade scipts after a forced upgrade if the bundled database file is not the latest version

    don't skip .sql upgrade scipts after a forced upgrade if the bundled database file is not the latest version

    I don't always update the bundled .db file when I add a new schema version but instead I add an upgrade script .sql file (e.g. database.db_upgrade_2-3.sql) and accodingly increment (e.g. 2->3) the version parameter when calling the super constructor SQLiteAssetHelper(...). This way existing installs use the .sql script as usual and new installs copy the bundled database.db (version 2) and then execute the .sql upgrade on top of that.

    I found a problem when I use setForcedUpgrade(2), now updates from version 1 to 3 would replace the old version 1 database with the bundled version 2 (like expected), but not apply the version 3 upgrade .sql file and instead just set the version to 3.

    I found that the code in getWritableDatabase() blindly overrides the version of the newly copied database to be the latest version mNewVersion that had been defined by calling the super constructor. I based this PR on top of #87 because that is also related to using setForcedUpgrade(). The main change in this PR is to remove the line db.setVersion(mNewVersion); in order to still trigger the version check and upgrade using .sql files if the bundled sqlite.db file is "outdated".

    opened by p-schneider 1
  • fixed memory leaks; Update SQLiteAssetHelper.java

    fixed memory leaks; Update SQLiteAssetHelper.java

    fixes some memory leaks

    Thanks to CoolMind. contribution source: https://github.com/jgilfelt/android-sqlite-asset-helper/issues/59 https://github.com/jgilfelt/android-sqlite-asset-helper/issues/62

    opened by mantas84 2
  • added a method to import sql scripts instead of copying predefined

    added a method to import sql scripts instead of copying predefined

    Hi Jeff, I think I messed with whitespaces, so ?w=1 should do it https://github.com/RookieGuy/android-sqlite-asset-helper/compare/jgilfelt:master...master?w=1

    How about merging this if you like what you see? Regards Klaus

    opened by uc-mobileapps-zz 0
Releases(v2.0.1)
Owner
Jeff Gilfelt
Jeff Gilfelt
A blazing fast, powerful, and very simple ORM android database library that writes database code for you.

README DBFlow is fast, efficient, and feature-rich Kotlin database library built on SQLite for Android. DBFlow utilizes annotation processing to gener

Andrew Grosner 4.9k Dec 30, 2022
A Java/Kotlin library for Android platform, to manage bean's persistence in SQLite, SharedPreferences, JSON, XML, Properties, Yaml, CBOR.

Thanks to JetBrains for support Kripton Persistence Library project! Kripton Persistence Library Kripton is a java library, for Android platform, that

xcesco 117 Nov 11, 2022
Collection of Kotlin APIs/tools to make using Realm Mobile database easier

Compass Kotlin API and tools to make working with Realm easier Components Compass is designed to make working with Realm easier through collection of

Arunkumar 16 Oct 4, 2022
SquiDB is a SQLite database library for Android and iOS

Most ongoing development is currently taking place on the dev_4.0 branch. Click here to see the latest changes and try out the 4.0 beta. Introducing S

Yahoo 1.3k Dec 26, 2022
A simple ToDo app to demonstrate the use of Realm Database in android to perform some basic CRUD operations like Create, Update and Delete.

Creating a Realm Model Class @RealmClass open class Note() : RealmModel { @PrimaryKey var id: String = "" @Required var title: String

Joel Kanyi 15 Dec 18, 2022
Sprinkles is a boiler-plate-reduction-library for dealing with databases in android applications

Sprinkles Sprinkles is a boiler-plate-reduction-library for dealing with databases in android applications. Some would call it a kind of ORM but I don

Emil Sjölander 781 Nov 28, 2022
ORMDroid is a simple ORM persistence framework for your Android applications.

ORMDroid is a simple ORM persistence framework for your Android applications, providing an easy to use, almost-zero-config way to handle model persist

Ross Bamford 87 Nov 10, 2022
Insanely easy way to work with Android Database.

Sugar ORM Insanely easy way to work with Android databases. Official documentation can be found here - Check some examples below. The example applicat

null 2.6k Dec 16, 2022
An Android library that makes developers use SQLite database extremely easy.

LitePal for Android 中文文档 LitePal is an open source Android library that allows developers to use SQLite database extremely easy. You can finish most o

Lin Guo 7.9k Dec 31, 2022
Insanely easy way to work with Android Database.

Sugar ORM Insanely easy way to work with Android databases. Official documentation can be found here - Check some examples below. The example applicat

null 2.6k Jan 9, 2023
Compile time processed, annotation driven, no reflection SQLite database layer for Android

SqliteMagic Simple yet powerful SQLite database layer for Android that makes database handling feel like magic. Overview: Simple, intuitive & typesafe

Siim Kinks 118 Dec 22, 2022
android 数据库框架,sqlite database

DBExecutor 主要的功能 1.使用了读写锁,支持多线程操作数据。 2.支持事务 3.支持ORM 4.缓存Sql,缓存表结构 这个类库主要用于android 数据库操作。 始终围绕着一个类对应一个表的概念。 只要创建一个实体类,就不用当心它怎么存储在数据库中,不用重新写增删改查的代码。基本的功

null 77 May 31, 2021
LiteOrm is a fast, small, powerful ORM framework for Android. LiteOrm makes you do CRUD operarions on SQLite database with a sigle line of code efficiently.

#LiteOrm:Android高性能数据库框架 A fast, small, powerful ORM framework for Android. LiteOrm makes you do CRUD operarions on SQLite database with a sigle line

马天宇 1.5k Nov 19, 2022
An Android library that makes developers use SQLite database extremely easy.

LitePal for Android 中文文档 LitePal is an open source Android library that allows developers to use SQLite database extremely easy. You can finish most o

Lin Guo 7.9k Jan 4, 2023
An app with implementation of Room database for Android platform

Room Room An app with implementation of Room database for Android platform The Room persistence library provides an abstraction layer over SQLite to a

Void Hash 1 Jan 4, 2023
a 3d database ORM experiment. (used in two commercial projects)

Android-TriOrm a 3d database ORM experiment for Android. (used in two commercial projects). based around small tables concept and JVM Serialization. H

Tomer Shalev 19 Nov 24, 2021
Realm is a mobile database: a replacement for SQLite & ORMs

Realm is a mobile database that runs directly inside phones, tablets or wearables. This repository holds the source code for the Java version of Realm

Realm 11.4k Jan 2, 2023
Room Database Queries with Kotlin Flow

Room Database Queries with Flow This app displays a list of bus stops and arrival times. Tapping a bus stop on the first screen will display a list of

asifj96 0 Apr 26, 2022
A simple NoSQL client for Android. Meant as a document store using key/value pairs and some rudimentary querying. Useful for avoiding the hassle of SQL code.

SimpleNoSQL A simple NoSQL client for Android. If you ever wanted to just save some data but didn't really want to worry about where it was going to b

Colin Miller 389 Sep 25, 2022