Simple builder for SQLite .
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'
}- 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)"
)
public class Table1{
//your code
}- Make Instance Class
DBInstanceextendsSQLiteBuilder
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
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)
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
}...
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
}
}...
public class DBInstance extends SQLiteBuilder {
...
public boolean deleteRootDb(Context context) {
String DB_NAME = "MyLibSQLiteSimple.db";
return deleteDatabaseOnRoot(context, DB_NAME);
}
}...
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);
}
}...
public class DBInstance extends SQLiteBuilder {
...
public boolean isDBExist() {
String DB_PATH_EXTERNAL = Environment.getExternalStorageDirectory().toString()
+ "/MyLibSQLiteExternal/MyLibSQLiteSimple.db";
return isDatabaseExists(DB_PATH_EXTERNAL);
}
}...
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
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 branch open-pull.
Copyright 2021 M. Fadli Zein





