https://github.com/petruki/dblite-library
DbLite is a simple and versatile SQLite interface
https://github.com/petruki/dblite-library
android-library sqlite
Last synced: about 1 month ago
JSON representation
DbLite is a simple and versatile SQLite interface
- Host: GitHub
- URL: https://github.com/petruki/dblite-library
- Owner: petruki
- License: mit
- Created: 2021-03-07T21:08:26.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-03-09T22:45:27.000Z (over 5 years ago)
- Last Synced: 2025-02-06T05:35:18.548Z (over 1 year ago)
- Topics: android-library, sqlite
- Language: Java
- Homepage:
- Size: 165 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
[](https://jitpack.io/#petruki/dblite-library)
### About
DbLite is a simple Android library that allows you to interact with SQLite DB.
Most of the libraries provide better and clean implementaion to interact with SQLite, but they lack of features when the data can be both relational and non-relational. This common scenario happens when you consume JSON data from external APIs and need to store it on a local DB.
This solution provides with a versatile way to implement and resolve model object dependencies easier.
Here are some of the features:
- Independent object persistence implementation (Wrappers)
- CRUD operations out-of-the-box
- Entity resolver for creating relationship from non-relational data
### Installation
> Step 1 - Add Jitpack to the project build.gradle file
```
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
```
> Step 2 - Add DbLite dependency to the module app of your project
```
dependencies {
implementation 'com.github.petruki:dblite-library:0.1.1-rc'
}
```
### How-to (Sample usage)
Below is the step-by-step you must follow to implement new Repository classes.
You can also check the playground demo included to this library source code for more detail.
1. Create model class: model/User.java
```java
public class User {
String _id;
String name;
String email;
}
```
2. Create wrapper class: repository/UserWrapper.java
```java
@DbLiteWrapper(entityName = "USER", columns = { "id", "name", "email" })
public class UserWrapper implements EntityWrapper {
@Override
public User unWrap(Cursor cursor) {
User user = new User();
user.setId(getString(cursor, "id"));
user.setName(getString(cursor, "name"));
user.setEmail(getString(cursor, "email"));
return user;
}
@Override
public ContentValues wrap(User user) {
ContentValues values = new ContentValues();
values.put("id", user.getId());
values.put("name", user.getName());
values.put("email", user.getEmail());
return values;
}
}
```
3. Create repository class: repository/UserRepository.java
```java
public class UserRepository extends AbstractRepository {
public UserRepository(Context context) {
super(context, new UserWrapper(), MyDatabase.class);
}
}
```
4. Create DB class that configures wrappers and DB arguments (repository/MyDatabase.java)
```java
@DbLite(dbName = "BOOKING_DB", version = 1, wrappers = {
UserWrapper.class,
BookingWrapper.class
})
abstract class MyDatabase extends DbLiteFactory {
protected MyDatabase(Context context) {
super(context);
}
}
```