Skip to content

Repository files navigation

SQLiteBuilder


Simple builder for SQLite .


Content List


Download

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

// build.gradle projectallprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
// build.gradle app/moduledependencies {
...
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)"
)
publicclassTable1{
//your code
}

  • Make Instance Class DBInstance extends SQLiteBuilder
publicclassDBInstanceextendsSQLiteBuilder {
}

  • Define your Table Class
@SQLiteDatabaseEntity(entities = {
Table1.class
})
publicclassDBInstanceextendsSQLiteBuilder {
...
}

  • Make function instance in DBInstance.
@SQLiteDatabaseEntity(entities = {
Table1.class
})
publicclassDBInstanceextendsSQLiteBuilder {
privatestaticSQLiteDatabasesqLiteDatabase;
publicstaticStringDB_NAME = "MyLibSQLiteSimple.db";
publicstaticSQLiteDatabasegetDataBase(Contextcontext) {
sqLiteDatabase = SQLiteBuilder.builder(DBInstance.class, context)
.setDatabaseName(DB_NAME)
.setDatabaseVersion(1)
.build();
returnsqLiteDatabase;
}
}

  • 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:
SQLiteDatabasesqLiteDatabase = 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).

privatestaticSQLiteDatabasesqLiteDatabase;
publicstaticSQLiteDatabasegetDataBase(Contextcontext) {
StringDB_PATH_BC = Environment.getExternalStorageDirectory().toString()
+ "/MyLibSQLiteBC/MyLibSQLiteSimple.db";
sqLiteDatabase = SQLiteBuilder.builder(DBInstance.class, context)
...
.putDatabaseToExternal(DB_PATH_BC)
...
.build();
returnsqLiteDatabase;
}

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 fileFrom External you can add and use function loadDatabaseFromExternal(DB_PATH_EXTERNAL) in SQLiteBuilder.builder(DBInstance.class, context).

privatestaticSQLiteDatabasesqLiteDatabase;
publicstaticSQLiteDatabasegetDataBase(Contextcontext) {
StringDB_PATH_EXTERNAL = Environment.getExternalStorageDirectory().toString()
+ "/MyLibSQLiteExternal/MyLibSQLiteSimple.db";
sqLiteDatabase = SQLiteBuilder.builder(DBInstance.class, context)
...
.loadDatabaseFromExternal(DB_PATH_EXTERNAL)
...
.build();
returnsqLiteDatabase;
}

File database will be Load from your Database Path.

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

You can write Class Table like this

@CreateTableQuerypublicclassTable1{
//your code
}

Delete Database on External

...
publicclassDBInstanceextendsSQLiteBuilder {
...
publicbooleandelete() {
StringDB_PATH_EXTERNAL = Environment.getExternalStorageDirectory().toString()
+ "/MyLibSQLiteExternal/MyLibSQLiteSimple.db";
returndeleteDatabase(DB_PATH_EXTERNAL); // return true/false
}
}

Delete Database on Root

...
publicclassDBInstanceextendsSQLiteBuilder {
...
publicbooleandeleteRootDb(Contextcontext) {
StringDB_NAME = "MyLibSQLiteSimple.db";
returndeleteDatabaseOnRoot(context, DB_NAME);
}
}

Backup Database From Root To External

...
publicclassDBInstanceextendsSQLiteBuilder {
...
publicbooleanbackUp(Contextcontext) {
StringBACK_UP_TO = Environment.getExternalStorageDirectory().toString()
+ "/MyLibSQLiteExternalBackUp";
StringDB_NAME = "MyLibSQLiteSimple.db";
returnbackUpDatabase(context, BACK_UP_TO, DB_NAME);
}
}

Check File Database Exists On External

...
publicclassDBInstanceextendsSQLiteBuilder {
...
publicbooleanisDBExist() {
StringDB_PATH_EXTERNAL = Environment.getExternalStorageDirectory().toString()
+ "/MyLibSQLiteExternal/MyLibSQLiteSimple.db";
returnisDatabaseExists(DB_PATH_EXTERNAL);
}
}

Check File Database Exists On Root

...
publicclassDBInstanceextendsSQLiteBuilder {
...
publicbooleanisDBExistOnRoot(Contextcontext){
StringDB_NAME = "MyLibSQLiteSimple.db";
returnisDatabaseExistOnRoot(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 branchopen-pull.


Copyright 2021 M. Fadli Zein

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages