Android Open Source - SQLite-Simple-Android S Q Lite Simple Helper






From Project

Back to project page SQLite-Simple-Android.

License

The source code is released under:

Apache License

If you think the Android project SQLite-Simple-Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package garin.artemiy.sqlitesimple.library;
/*w  w w.  j a  v  a  2s. co m*/
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import garin.artemiy.sqlitesimple.library.util.SimpleConstants;
import garin.artemiy.sqlitesimple.library.util.SimpleDatabaseUtil;
import garin.artemiy.sqlitesimple.library.util.SimplePreferencesHelper;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

/**
 * author: Artemiy Garin
 * Copyright (C) 2013 SQLite Simple 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.
 */
@SuppressWarnings({"BooleanMethodIsAlwaysInverted", "WeakerAccess", "CanBeFinal"})
public class SQLiteSimpleHelper extends SQLiteOpenHelper {

    private String assetsDatabaseName;
    private Context context;
    private SimplePreferencesHelper sharedPreferencesUtil;
    private String sharedPreferencesPlace;
    private static final Object writableObjectLock = new Object();
    private static final Object readableObjectLock = new Object();

    /**
     * @param context                - see {@link android.content.Context}
     * @param sharedPreferencesPlace - see {@link garin.artemiy.sqlitesimple.library.util.SimpleConstants}
     * @param databaseVersion        - see {@link android.database.sqlite.SQLiteOpenHelper}
     * @param assetsDatabaseName     - Load assets or internal (if null) sqlite database if need
     * @param isFTS                  - see {@link garin.artemiy.sqlitesimple.library.SQLiteSimpleFTS}
     */
    public SQLiteSimpleHelper(Context context, String sharedPreferencesPlace,
                              int databaseVersion, String assetsDatabaseName, boolean isFTS) {
        super(context, SimpleDatabaseUtil.getFullDatabaseName(assetsDatabaseName, context, isFTS), null, databaseVersion);
        this.assetsDatabaseName = assetsDatabaseName;
        this.context = context;
        this.sharedPreferencesPlace = sharedPreferencesPlace;
        sharedPreferencesUtil = new SimplePreferencesHelper(context);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        List<String> sqlQueries = sharedPreferencesUtil.getList(
                String.format(SimplePreferencesHelper.DATABASE_QUERIES, sharedPreferencesPlace));
        if (sqlQueries != null) for (String sqlQuery : sqlQueries)
            sqLiteDatabase.execSQL(sqlQuery);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
        List<String> tables = sharedPreferencesUtil.getList(
                String.format(SimplePreferencesHelper.DATABASE_TABLES, SimplePreferencesHelper.LOCAL_PREFERENCES));
        if (tables != null) for (String table : tables)
            sqLiteDatabase.execSQL(String.format(SimpleConstants.FORMAT_TWINS,
                    SimpleConstants.SQL_DROP_TABLE_IF_EXISTS, table));

        onCreate(sqLiteDatabase);
    }

    @Override
    public SQLiteDatabase getWritableDatabase() {
        synchronized (writableObjectLock) {
            checkDatabaseFromAssets();
            if (assetsDatabaseName == null) return super.getWritableDatabase();
            else return SQLiteDatabase.openDatabase(SimpleDatabaseUtil.
                    getFullDatabasePath(context, assetsDatabaseName), null, SQLiteDatabase.OPEN_READWRITE);
        }
    }

    @Override
    public SQLiteDatabase getReadableDatabase() {
        synchronized (readableObjectLock) {
            checkDatabaseFromAssets();
            if (assetsDatabaseName == null) return super.getReadableDatabase();
            else return SQLiteDatabase.openDatabase(SimpleDatabaseUtil.
                    getFullDatabasePath(context, assetsDatabaseName), null, SQLiteDatabase.OPEN_READONLY);
        }
    }

    private void checkDatabaseFromAssets() {
        if (assetsDatabaseName != null && !isDatabaseExist()) {
            super.getWritableDatabase(); // create empty database
            super.close();
            copyDatabaseFromAssets();
        }
    }

    private void copyDatabaseFromAssets() {
        try {
            InputStream inputStream = context.getAssets().open(assetsDatabaseName);
            OutputStream outputStream = new FileOutputStream(SimpleDatabaseUtil.getFullDatabasePath(context,
                    assetsDatabaseName));

            byte[] buffer = new byte[1024];
            int length;

            while ((length = inputStream.read(buffer)) > 0) outputStream.write(buffer, 0, length);

            outputStream.flush();
            outputStream.close();
            inputStream.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private boolean isDatabaseExist() {
        return new File(SimpleDatabaseUtil.getFullDatabasePath(context, assetsDatabaseName)).exists();
    }

}




Java Source Code List

garin.artemiy.sqlitesimple.example.MainActivity.java
garin.artemiy.sqlitesimple.example.MainApplication.java
garin.artemiy.sqlitesimple.example.adapters.FTSAdapter.java
garin.artemiy.sqlitesimple.example.adapters.RecordAdapter.java
garin.artemiy.sqlitesimple.example.dao.InternalDAO.java
garin.artemiy.sqlitesimple.example.dao.RecordsDAO.java
garin.artemiy.sqlitesimple.example.models.InternalRecord.java
garin.artemiy.sqlitesimple.example.models.Record.java
garin.artemiy.sqlitesimple.library.SQLiteSimpleDAO.java
garin.artemiy.sqlitesimple.library.SQLiteSimpleFTS.java
garin.artemiy.sqlitesimple.library.SQLiteSimpleHelper.java
garin.artemiy.sqlitesimple.library.SQLiteSimple.java
garin.artemiy.sqlitesimple.library.annotations.Column.java
garin.artemiy.sqlitesimple.library.annotations.Table.java
garin.artemiy.sqlitesimple.library.model.FTSModel.java
garin.artemiy.sqlitesimple.library.util.ColumnType.java
garin.artemiy.sqlitesimple.library.util.SimpleConstants.java
garin.artemiy.sqlitesimple.library.util.SimpleDatabaseUtil.java
garin.artemiy.sqlitesimple.library.util.SimplePreferencesHelper.java