An open API service indexing awesome lists of open source software.

https://github.com/lemberg/android-database

Sample of a project that deals with database
https://github.com/lemberg/android-database

Last synced: 8 months ago
JSON representation

Sample of a project that deals with database

Awesome Lists containing this project

README

          

**SQLite database sample**

This sample will show you an easy way to:

1. create and register one or several databases;
2. manage operations with database and its tables;
3. handle database migration tasks;
4. save/load data in one line.

**Implementation guide**

To implement the following database structure in your app, you need pay your attention on 3 main classes: `BaseSQLiteOpenHelper`, `DatabaseRegister`, `BaseDAO`.

1. Make your `DatabaseHelper` extends `BaseSQLiteOpenHelper` and override all the methods to configure your database.
`MigratableSQLiteOpenHelper` helps to manage data migration while database upgrading or downgrading.

2. In your `DatabaseManager` create a global instance of `DatabaseRegister
class` and `registerDatabase(BaseSQLiteOpenHelper sqLiteOpenHelper)` method.

3. In Application class you will now be able to init your
`DatabaseManager` and `registerDatabase(new DatabaseHelper())`

4. The next step is implementation of functionality to access data of
database. To make it work just `extend BaseDAO` which
adds ability to access data of database table. Using DAO object you can save, update, delete, select data of database table. Feel free, you can also add additional functionality into DAO.

**Usage guide**

Using this database model, makes your calls to database easy and simple, here how it would looks like:

```java
private void saveContacts() {
List contacts = generateContactList();
DatabaseManager.getInstance().addContacts(contacts);
}

private List loadContacts() {
return DatabaseManager.getInstance().loadContacts();
}
```

**More Info**

If you want to see full code, check out sample app.

Also please check unit tests [section](https://github.com/lemberg/android-database/wiki/unit-tests).

**License**
```
The MIT License (MIT)

Copyright (c) 2016 Lemberg Solutions Ltd

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
```