Android Handy SQLite Helper Library

A library for shipping SQLite databases within application assets and optionally placing them in external storage

License

License

Categories

Categories

SQLite Data Databases
GroupId

GroupId

org.deejdev.database.handysqlite
ArtifactId

ArtifactId

handysqlite
Last Version

Last Version

1.0
Release Date

Release Date

Type

Type

aar
Description

Description

Android Handy SQLite Helper Library
A library for shipping SQLite databases within application assets and optionally placing them in external storage
Project URL

Project URL

https://github.com/ultimate-deej/Android-Handy-SQLite-Helper
Source Code Management

Source Code Management

https://github.com/ultimate-deej/Android-Handy-SQLite-Helper

Download handysqlite

How to add to project

<!-- https://jarcasting.com/artifacts/org.deejdev.database.handysqlite/handysqlite/ -->
<dependency>
    <groupId>org.deejdev.database.handysqlite</groupId>
    <artifactId>handysqlite</artifactId>
    <version>1.0</version>
    <type>aar</type>
</dependency>
// https://jarcasting.com/artifacts/org.deejdev.database.handysqlite/handysqlite/
implementation 'org.deejdev.database.handysqlite:handysqlite:1.0'
// https://jarcasting.com/artifacts/org.deejdev.database.handysqlite/handysqlite/
implementation ("org.deejdev.database.handysqlite:handysqlite:1.0")
'org.deejdev.database.handysqlite:handysqlite:aar:1.0'
<dependency org="org.deejdev.database.handysqlite" name="handysqlite" rev="1.0">
  <artifact name="handysqlite" type="aar" />
</dependency>
@Grapes(
@Grab(group='org.deejdev.database.handysqlite', module='handysqlite', version='1.0')
)
libraryDependencies += "org.deejdev.database.handysqlite" % "handysqlite" % "1.0"
[org.deejdev.database.handysqlite/handysqlite "1.0"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

Handy SQLite Helper for Android

A library for shipping SQLite databases within application assets and optionally placing them in external storage

Setup

Maven

<dependency>
    <groupId>org.deejdev.database.handysqlite</groupId>
    <artifactId>handysqlite</artifactId>
    <version>1.0</version>
    <type>aar</type>
</dependency>

Gradle

dependencies {
    ...
    compile 'org.deejdev.database.handysqlite:handysqlite:1.0'
}

Usage

Using the library is similar to using framework's SQLiteOpenHelper. Create a subclass of HandySQLiteHelper as you do when using SQLiteOpenHelper, and pass context, InputStream, destination File and version to constructor.

Examples

Basic usage. Database is read from asset and extracted into standard database directory.

public class OpenHelper extends HandySQLiteHelper {
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "db.sqlite"; // both asset name and destination file name

    public OpenHelper(Context context, InputStream input, File destinationPath, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, getInput(context), context.getDatabasePath(DATABASE_NAME), null, DATABASE_VERSION);
    }

    private static InputStream getInput(Context context) {
        try {
            AssetManager assets = context.getAssets();
            return assets.open(DATABASE_NAME);
        } catch (IOException e) {
            // Should not happen normally
            throw new HandySQLiteHelperException("Error reading database from assets", e);
        }
    }
}

To place database in external storage specify a corresponding path

public OpenHelper(Context context, InputStream input, File destinationPath, SQLiteDatabase.CursorFactory factory, int version) {
    super(context, getInput(context), new File("/mnt/sdcard/some/random/path"), null, DATABASE_VERSION);
}

If you are developing for Android 2.2, and your database is larger than 1MB, consider splitting it into smaller chunks. See this issue

private static InputStream getInput(Context context) {
    try {
        AssetManager assets = context.getAssets();
        ArrayList<InputStream> chunks = new ArrayList<InputStream>();
        chunks.add(assets.open("database_aa"));
        chunks.add(assets.open("database_ab"));
        chunks.add(assets.open("database_ac"));
        chunks.add(assets.open("database_ad"));
        return new SequenceInputStream(Collections.enumeration(chunks));
    } catch (IOException e) {
        // Should not happen normally
        throw new EmbeddedSQLiteException("Error reading database from assets", e);
    }
}

License

Copyright 2014 Maxim Naumov

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.

Versions

Version
1.0