Simple builder for SQLite .
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'
}- Make Class Table
- Usage
- File Database On External
- Load Database From External
- Delete Database On External
- Delete Database On Root
- Backup Database From Root To External
- Check File Database Exists On External
- Check File Database Exists On Root
- 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
DBInstanceextendsSQLiteBuilder
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
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)
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
}...
publicclassDBInstanceextendsSQLiteBuilder {
...
publicbooleandelete() {
StringDB_PATH_EXTERNAL = Environment.getExternalStorageDirectory().toString()
+ "/MyLibSQLiteExternal/MyLibSQLiteSimple.db";
returndeleteDatabase(DB_PATH_EXTERNAL); // return true/false
}
}...
publicclassDBInstanceextendsSQLiteBuilder {
...
publicbooleandeleteRootDb(Contextcontext) {
StringDB_NAME = "MyLibSQLiteSimple.db";
returndeleteDatabaseOnRoot(context, DB_NAME);
}
}...
publicclassDBInstanceextendsSQLiteBuilder {
...
publicbooleanbackUp(Contextcontext) {
StringBACK_UP_TO = Environment.getExternalStorageDirectory().toString()
+ "/MyLibSQLiteExternalBackUp";
StringDB_NAME = "MyLibSQLiteSimple.db";
returnbackUpDatabase(context, BACK_UP_TO, DB_NAME);
}
}...
publicclassDBInstanceextendsSQLiteBuilder {
...
publicbooleanisDBExist() {
StringDB_PATH_EXTERNAL = Environment.getExternalStorageDirectory().toString()
+ "/MyLibSQLiteExternal/MyLibSQLiteSimple.db";
returnisDatabaseExists(DB_PATH_EXTERNAL);
}
}...
publicclassDBInstanceextendsSQLiteBuilder {
...
publicbooleanisDBExistOnRoot(Contextcontext){
StringDB_NAME = "MyLibSQLiteSimple.db";
returnisDatabaseExistOnRoot(context, DB_NAME);
}
}You can combine this library with EasySQLiteCRUD
Sample APP, just clone it Java & Kotlin
- 1.0.1
- First Release
- 1.0.2
- Bug Fixing
- 1.0.3
- Ready Release
- 2.0.0
- Suppoert SDK 16
You can sent your constibution to branchopen-pull.
Copyright 2021 M. Fadli Zein





