| Before | Simple Code |
| Before | Simple Code |
I just try to make Cursor and ContentValues disappeared but its still in there, I hate always having Typo on my code, specially on Cursor and ContentValues again and again. And take time to debug and fix it. arghh...
Than I made this so I don't hate it anymore, hope you enjoy it :)
please tell me if you find error.
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:EasySQLiteCRUD:version'
}- 1. Table
- 2. Entity
- 3. Insert
- 4. Update
- 5. Delete
- 6. Count
- 7. Read
- 8. Query Data for Complex Query. return List.
- 9. Query Result return true/false.
- 10. Read Single Data
- 11. Insert Or Ignore
- 12. Insert Or Update
- 13. Update And Backup Last Data
- 14. Get Last Data
- Create Table
Please make sure you have access to your database with instance from SQLiteDatabase. example :
public class TestActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
...
DatabaseHelper databaseHelper = new DatabaseHelper(getApplicationContext());
SQLiteDatabase database = databaseHelper.getWritableDatabase();
Table1 table1 = new Table1(database);
List<Table1> listData = table1.read(); //example calling function
...
}
}Here is my DBInstance.
Or you can use your own configuration to connect to Database, just make sure you have access to your local database. ReadMore.
You need to extends SQLiteLIB<YourEntity> to your Entity Class. And Use Annotation @SQLiteTable(tableName = "your_table_name"). Then make contructor like this:
@SQLiteTable(tableName = "table1")
public class Table1 extends SQLiteLIB<Table1> {
...
private SQLiteDatabase sqLiteDatabase;
public Table1() {}
public Table1(SQLiteDatabase sqLiteDatabase) {
this.sqLiteDatabase = sqLiteDatabase;
}
...
}Lets see Boilerplate Code Entity
Simple Code
Declare Entity. You can make it more simple with this Annotation
@PrimaryKeyTypeDataor@VarcharTypeDataor@IntegerTypeDataor@TimeStampTypeDataor@TextTypeDataor@DoubleTypeDataor@OtherTableData
@SQLiteTable(tableName = "table1")
public class Table1 extends SQLiteLIB<Table1> {
@PrimaryKeyTypeData private int id; // for Primary key
// Default AutoIncrement true
// @PrimaryKeyTypeData(autoGenerate = false) to disable
@VarcharTypeData private String name; // for Varchar
@DecimalTypeData private double rating; // for Decimal/Real
@TextTypeData private String desc; // for String
@IntegerTypeData private int flag_active; // for Integer
@TimeStampTypeData private String created_at; // for String
// To use Current time
// @TimeStampTypeData(currentTime = true)
// work only on insertData(..,..,..) and if created_at is null
// Format time yyyy-MM-dd HH:mm:ss
@OtherTableData(withTable = "table2", alias = "id_table1")
private String id_table1;
private SQLiteDatabase sqLiteDatabase;
public Table1() {}
public Table1(SQLiteDatabase sqLiteDatabase) {
this.sqLiteDatabase = sqLiteDatabase;
}
//getter setter
...
}Notes :
@PrimaryKeyTypeData: Your variable type shouldint.@VarcharTypeData: Your variable type shouldString.@IntegerTypeData: Your variable type shouldint.@TimeStampTypeData: Your variable type shouldString.@TextTypeData: Your variable type shouldString.@DoubleTypeData: Your variable type shoulddouble.@OtherTableData:withTable= other table to join with current table.alias= columnname.
@DefaultData: to set Default value.- Work only On
@VarcharTypeData,@TextTypeData. - example
- Work only On
@VarcharTypeData
@DefaultData(value = "default data")
private String name;
@TextTypeData
@DefaultData(value = "default data")
private String desc;Lets see Boilerplate Code Insert Data
Simple Code
@SQLiteTable(tableName = "table1")
public class Table1 extends SQLiteLIB<Table1> {
...
//INSERT INTO table1 (name, rating, desc, flag_active, created_at)
//VALUES ('Zein', '10.0.', 'Android Programmer', '1', '12-12-2020');
public boolean insert() {
Table1 data = new Table1();
data.setName("Zein");
data.setRating(10.0);
data.setDesc("Android Programmer");
data.setFlag_active(1);
data.setCreated_at("12-12-2020");
return insertData(Table1.class, sqLiteDatabase, data);
}
}Lets see Boilerplate Code Update Data
Simple Code
@SQLiteTable(tableName = "table1")
public class Table1 extends SQLiteLIB<Table1> {
...
//UPDATE table1 SET name='Name Update', desc='Desc Update', flag_active='0' WHERE id='1';
public boolean update() {
//set your value to update
Table1 data = new Table1();
data.setName("Name Update");
data.setDesc("Desc Update");
data.setFlag_active(0);
//String condition = ""; //to update all data
//String condition = "WHERE 1"; //to update all data
String condition = "WHERE id='1'"; //for single condition
//String condition = "WHERE id='1' AND flag_Active='1'"; //for multi condition
String[] fieldToUpdate = new String[]{
"name",
"desc",
"flag_active"
}; // put all field that you want to update
return updatedData(Table1.class, sqLiteDatabase, data, condition, fieldToUpdate); // return true/false
}
}Lets see Boilerplate Code Delete Data
Simple Code
@SQLiteTable(tableName = "table1")
public class Table1 extends SQLiteLIB<Table1> {
...
//DELETE FROM table1 WHERE id='1';
public boolean delete() {
//String condition = ""; //to delete all data
//String condition = "WHERE 1"; //to delete all data
String condition = "WHERE id='1'"; //for single condition
//String condition = "WHERE id='1' AND flag_Active='1'"; //for multi condition
return deleteData(Table1.class, sqLiteDatabase, condition);
}
}Lets see Boilerplate Code Count Data
Simple Code
@SQLiteTable(tableName = "table1")
public class Table1 extends SQLiteLIB<Table1> {
...
//type 1 SELECT COUNT(*) FROM table1;
public int count() {
return countData(Table1.class, sqLiteDatabase);
}
//type 2 SELECT COUNT(*) FROM table1 WHERE flag_Active='1';
public int count2() {
//String condition = "WHERE 1"; //count all
String condition = "WHERE flag_active='1'"; //for single condition
//String condition = "WHERE id='1' AND flag_Active='1'"; //for multi condition
return countData(Table1.class, sqLiteDatabase, condition);
}
//type 3 Your Custom Query
//SELECT COUNT(id) FROM table1;
public int queryCount() {
String query = "SELECT COUNT(id) FROM table1;";
return queryCount(Table1.class, sqLiteDatabase, query);
}
}Lets see Boilerplate Code Read Data
Simple Code
@SQLiteTable(tableName = "table1")
public class Table1 extends SQLiteLIB<Table1> {
...
//type 1 SElECT * FROM table1;
public List<Table1> read() {
return readData(Table1.class, sqLiteDatabase);
}
//type 2 SELECT * FROM table1 WHERE flag_active='1';
public List<Table1> read2() {
//String condition = ""; //read all
//String condition = "WHERE 1"; //read all
String condition = "WHERE flag_active='1'"; //for single condition
//String condition = "WHERE id='1' AND flag_Active='1'"; //for multi condition
return readData(Table1.class, sqLiteDatabase, condition);
}
}Lets see Boilerplate Code Query Data
Simple Code
@SQLiteTable(tableName = "table1")
public class Table1 extends SQLiteLIB<Table1> {
...
//dont forget to write this to
@OtherTableData(withTable = "table2", alias = "id_table1")
private String id_table1;
...
public List<Table1> query(){
String query ="SELECT table1.*, table2.id_table1 FROM table1 JOIN table2 ON table2.id_table1 = table1.id;";
return queryData(Table1.class, sqLiteDatabase, query);
}
}Lets see Boilerplate Code Query Result
Simple Code
@SQLiteTable(tableName = "table1")
public class Table1 extends SQLiteLIB<Table1> {
...
public boolean queryResultUpdate() {
String query = "UPDATE table1 SET flag_Active='2' WHERE id='1'";
return queryResult(sqLiteDatabase, query); //return true/false
}
}Notes :
You can use it to excecute update or delete query and give you true/false as return.
Lets see Boilerplate Code Read Single Data
Simple Code
@SQLiteTable(tableName = "table1")
public class Table1 extends SQLiteLIB<Table1> {
...
//type 1 SELECT * FROM table1 LIMIT 1;
public Table1 read3() {
return readSingleData(Table1.class, sqLiteDatabase);
}
//type 2 SELECT * FROM table1 WHERE flag_active='1' LIMIT 1;
public Table1 read4() {
//String condition = ""; //read single data
//String condition = "WHERE 1"; //read single data
String condition = "WHERE flag_active='1'"; //for single condition
//String condition = "WHERE id='1' AND flag_Active='1'"; //for multi condition
return readSingleData(Table1.class, sqLiteDatabase, condition);
}
}Notes : You have to validate result is null or not.
Table1 read3 = table1.read3();
if (read3!=null)
Log.d(TAG, "onCreate_12: " + read3.getName());
else
Log.d(TAG, "onCreate_12: " + "null");Lets see Boilerplate Code Insert Or Ignore
Simple Code
@SQLiteTable(tableName = "table1")
public class Table1 extends SQLiteLIB<Table1> {
...
//INSERT INTO table1 (id, name, rating, desc, flag_active, created_at) VALUES (6,'Zein', '10.0.', 'Android Programmer', '1', '12-12-2020');
public boolean insertOrIgnore() {
Table1 data = new Table1();
data.setId(6); //important line, please set your id first
data.setName("Zein");
data.setRating(10.0);
data.setDesc("Android Programmer");
data.setFlag_active(1);
data.setCreated_at("12-12-2020");
return insertDataOrIgnore(Table1.class, sqLiteDatabase, data);
}
//INSERT INTO table1 (id, name, rating, desc, flag_active, created_at) VALUES (8, 'Zein', '10.0.', 'Android Programmer', '1', '12-12-2020');
public boolean insertOrIgnoreQuery() {
Table1 data = new Table1();
data.setId(8);
data.setName("GZE8");
data.setRating(10.0);
data.setDesc("Android Programmer");
data.setFlag_active(1);
data.setCreated_at("12-12-2020");
String iqnoreIfQuery = "WHERE name='GZE8' and flag_active='1';";
return insertDataOrIgnore(Table1.class, sqLiteDatabase, data, iqnoreIfQuery);
}
}Notes :
You can use it to excecute insert or ignore and give you true/false as return. true = inserted, false = ignored.
Lets see Boilerplate Code Insert Or Update
Simple Code
@SQLiteTable(tableName = "table1")
public class Table1 extends SQLiteLIB<Table1> {
...
//INSERT INTO table1 (id, name, rating, desc, flag_active, created_at) VALUES (7, 'Name Update', '1.6.', 'Desc Update', '1', '12-12-2020');
//or
//UPDATE table1 SET name='Name Update', rating='1.6', desc='Desc Update', flag_active='1', created_at='12-12-2020' WHERE id='7';
public boolean insertOrUpdate() {
Table1 data = new Table1();
data.setId(7); //important line, please set your id first
data.setName("Name Update 2");
data.setRating(1.6);
data.setDesc("Desc Update 1");
data.setFlag_active(10);
data.setCreated_at("12-12-2020");
String[] fieldToUpdate = new String[]{
"name",
"rating",
"desc",
"flag_active",
"created_at"
}; // put all field that you want to update
return insertDataOrUpdate(Table1.class, sqLiteDatabase, data, fieldToUpdate);
}
//INSERT INTO table1 (id, name, rating, desc, flag_active, created_at) VALUES (9, 'Name Update', '1.6', 'Desc Update', '1', '12-12-2020');
//or
//UPDATE table1 SET name='Name Update', rating='1.6', desc='Desc Update', flag_active='1', created_at='12-12-2020' WHERE id='9' and flag_active='10';
public boolean insertOrUpdateQuery() {
Table1 data = new Table1();
data.setId(9);
data.setName("Name Update 10");
data.setRating(1.6);
data.setDesc("Desc Update 10");
data.setFlag_active(10);
data.setCreated_at("12-12-2020");
String[] fieldToUpdate = new String[]{
"name",
"rating",
"desc",
"flag_active",
"created_at"
}; // put all field that you want to update
String where = "WHERE id='9' and flag_active='10'";
return insertDataOrUpdate(Table1.class, sqLiteDatabase, data, fieldToUpdate, where);
}
}Notes :
You can use it to excecute insert or update and give you true/false as return.
First you have to make table history, and put it in Annotation @HistoryTable(tableName = "your_table_history_name").
Create table1_his that have same column with table1 and add one extra column id_edit at the beggining of the table1_his. History table should be like this.
Simple Code
@SQLiteTable(tableName = "table1")
@HistoryTable(tableName = "table1_his")
public class Table1 extends SQLiteLIB<Table1> {
...
//INSERT INTO table1 (id, name, rating, desc, flag_active, created_at) VALUES (10, 'Name 10', '1.6', 'Desc 10', '10', '12-12-2020');
//or if value exists
//UPDATE table1 SET name='Name 10', rating='1.6', desc='Desc 10', flag_active='10', created_at='12-12-2020' WHERE id='10';
//INSERT INTO table1_his (id, name, rating, desc, flag_active, created_at) VALUES (10, 'Name 10', '1.6', 'Desc 10', '10', '12-12-2020');
public boolean lastOnHistory() {
Table1 data = new Table1();
data.setId(10); //important line, please set your id first
data.setName("Name 10");
data.setRating(1.6);
data.setDesc("Desc 10");
data.setFlag_active(10);
data.setCreated_at("12-12-2020");
return lastDataOnHistory(Table1.class, sqLiteDatabase, data);
}
}Notes :
You can use it to insert new value. If value exists current data will be update and last data will insert to History Table.
Simple Code
@SQLiteTable(tableName = "table1")
public class Table1 extends SQLiteLIB<Table1> {
...
//SELECT * FROM table1 ORDER BY id DESC LIMIT 1;
public Table1 getLastData() {
return readLastData(Table1.class, sqLiteDatabase);
}
}Notes : You have to validate result is null or not.
Entity Old Verision Table1_OLD.java & Table2_OLD.java
Entity New Verion Table1.java & Table2.java
DatabaseHelper.java & DBInstance.java & TestActivity.java & activity_test.xml & gradle
Example App Java
You can combine this library with SQLiteBuilder
- 1.0.0
- First Release
- 1.0.6
- Kotlin Version
- 1.0.9
- Fixing Bug On Update
- 2.0.0
- Add Feature
queryResult()
- Add Feature
- 2.0.5
- Fixing Bug On Update Kotlin
- 2.0.6
- Remove Auto WHERE on All Query
- 2.0.7
- Add Feature
queryCount()
- Add Feature
- 2.0.8
- Bug Fixing
- 2.0.9
- Bug Fixing
- 2.1.0
- Bug Fixing
- 2.1.1
- Bug Fixing
- 3.0.0
- Support SDK 16
- 3.1.0
- Read Single Data
- Insert Or Ignore
- Insert Or Update
- 3.1.1
- Insert Or Ignore Condition
- Insert Or Update Condition
- 3.2.0
- Insert And Backup To History
- 3.3.0
- Read Last Data
- 3.3.1
- add feature
@DefaultData - add feature DefaultValue(CurrentTime) on
@TimeStampTypeData
- add feature
- 3.3.2
- remove feature
@JoinColumn - add feature
@OtherTableData
- remove feature
You can sent your constibution to branch open-pull.
Copyright 2020 M. Fadli Zein