Skip to content

gzeinnumer/SQLiteBuilder

Repository files navigation

SQLiteBuilder


Simple builder for SQLite .


Content List


Download

Add maven jitpack.io and dependencies in build.gradle (Project) :

// build.gradle project
allprojects {
  repositories {
    ...
    maven { url 'https://jitpack.io' }
  }
}

// build.gradle app/module
dependencies {
  ...
  implementation 'com.github.gzeinnumer:SQLiteBuilder:version'
}

Feature List


Tech stack and 3rd library


Usage

  • Make Class Table.

Example : Make class Table1 and put your query CREATE TABLE table1(...); to annotation @CreateTableQuery(query = { ... }).

@CreateTableQuery(
    query = "CREATE TABLE table1 (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, rating REAL, desc TEXT, flag_active INTEGER, created_at TEXT)"
)
public class Table1{

    //your code

}

  • Make Instance Class DBInstance extends SQLiteBuilder
public class DBInstance extends SQLiteBuilder {
}

  • Define your Table Class
@SQLiteDatabaseEntity(entities = {
        Table1.class
})
public class DBInstance extends SQLiteBuilder {

   ...

}

  • Make function instance in DBInstance.
@SQLiteDatabaseEntity(entities = {
        Table1.class
})
public class DBInstance extends SQLiteBuilder {

    private static SQLiteDatabase sqLiteDatabase;
    public static String DB_NAME = "MyLibSQLiteSimple.db";

    public static SQLiteDatabase getDataBase(Context context) {
        sqLiteDatabase = SQLiteBuilder.builder(DBInstance.class, context)
                .setDatabaseName(DB_NAME)
                .setDatabaseVersion(1)
                .build();
        return sqLiteDatabase;
    }

}

  • After you have defined the entity and make function getDataBase(Context context), you can use the following code to create an instance of the database:
SQLiteDatabase sqLiteDatabase = DBInstance.getDataBase(getApplicationContext());

Database and table created on Root Directory

File Database On External

If you want to PUT your database file on External you can add and use function putDatabaseToExternal(DB_PATH_BC) in SQLiteBuilder.builder(DBInstance.class, context).

private static SQLiteDatabase sqLiteDatabase;

public static SQLiteDatabase getDataBase(Context context) {
    String DB_PATH_BC = Environment.getExternalStorageDirectory().toString()
        + "/MyLibSQLiteBC/MyLibSQLiteSimple.db";

    sqLiteDatabase = SQLiteBuilder.builder(DBInstance.class, context)
            ...
            .putDatabaseToExternal(DB_PATH_BC)
            ...
            .build();
    return sqLiteDatabase;
}

File Database will be create on your folder Path. (Not in Root anymore like this)

Load Database From External

If you want to Load your database file From External you can add and use function loadDatabaseFromExternal(DB_PATH_EXTERNAL) in SQLiteBuilder.builder(DBInstance.class, context).

private static SQLiteDatabase sqLiteDatabase;

public static SQLiteDatabase getDataBase(Context context) {
    String DB_PATH_EXTERNAL = Environment.getExternalStorageDirectory().toString()
        + "/MyLibSQLiteExternal/MyLibSQLiteSimple.db";

    sqLiteDatabase = SQLiteBuilder.builder(DBInstance.class, context)
            ...
            .loadDatabaseFromExternal(DB_PATH_EXTERNAL)
            ...
            .build();
    return sqLiteDatabase;
}

File database will be Load from your Database Path.

Warning this method will ignore @CreateTableQuery(query = "")

You can write Class Table like this

@CreateTableQuery
public class Table1{
    //your code
}

Delete Database on External

...
public class DBInstance extends SQLiteBuilder {

    ...

    public boolean delete() {
        String DB_PATH_EXTERNAL = Environment.getExternalStorageDirectory().toString()
            + "/MyLibSQLiteExternal/MyLibSQLiteSimple.db";
        return deleteDatabase(DB_PATH_EXTERNAL); // return true/false
    }
}

Delete Database on Root

...
public class DBInstance extends SQLiteBuilder {

    ...

    public boolean deleteRootDb(Context context) {
        String DB_NAME = "MyLibSQLiteSimple.db";
        return deleteDatabaseOnRoot(context, DB_NAME);
    }
}

Backup Database From Root To External

...
public class DBInstance extends SQLiteBuilder {

    ...

    public boolean backUp(Context context) {
        String BACK_UP_TO = Environment.getExternalStorageDirectory().toString()
            + "/MyLibSQLiteExternalBackUp";
        String DB_NAME = "MyLibSQLiteSimple.db";
        return backUpDatabase(context, BACK_UP_TO, DB_NAME);
    }
}

Check File Database Exists On External

...
public class DBInstance extends SQLiteBuilder {

    ...

    public boolean isDBExist() {
        String DB_PATH_EXTERNAL = Environment.getExternalStorageDirectory().toString()
            + "/MyLibSQLiteExternal/MyLibSQLiteSimple.db";
        return isDatabaseExists(DB_PATH_EXTERNAL);
    }
}

Check File Database Exists On Root

...
public class DBInstance extends SQLiteBuilder {

    ...

    public boolean isDBExistOnRoot(Context context){
        String DB_NAME = "MyLibSQLiteSimple.db";
        return isDatabaseExistOnRoot(context, DB_NAME);
    }
}

You can combine this library with EasySQLiteCRUD


Example Code/App

Sample APP, just clone it Java & Kotlin

Sample Code And App


Version

  • 1.0.1
    • First Release
  • 1.0.2
    • Bug Fixing
  • 1.0.3
    • Ready Release
  • 2.0.0
    • Suppoert SDK 16

Contribution

You can sent your constibution to branch open-pull.


Copyright 2021 M. Fadli Zein

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages