Skip to content

sambhav2358/TinyDB

Repository files navigation

TinyDB

A simple and easy to use database library to save user data. It is very lighweight and uses on 8kb or less on the app! Also it needs minimum api level of 25 only which means it will work on 99.7% devices. Isn't it amazing?

API API GitHub issues GitHub forks GitHub stars

Recent updates

You can now create multiple databases. To use the default Datatabse, just call this:

public class MainActivity extends AppCompatActivity implements ValueChangeListener{

    TextView textView;
    TextView textView2;
    TinyDefaultDB defaultDB;

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.sampleTV);
        textView2 = findViewById(R.id.sampleTV2);

        defaultDB = TinyDB.getInstance().getDefaultDatabase(this);

        defaultDB.setValueChangeListener(this); // this will trigger when a value will be modified ot deleted
        defaultDB.putInt("abc",new Random().nextInt(10000));
        textView.setText(defaultDB.getInt("abc",1)+ "");
    }

    @Override
    public <E> void onValueAdded(String key, E value, String dbName) {
    }

    @Override
    public void onKeyRemoved(String key, String dbName) {
    }

    @Override
    public void onAllKeysRemoved(String dbName) {
    }
}

This will store the values in the default database, but, you want to store the different values in the same key right, so, for that, create another custom database like this:

public class MainActivity extends AppCompatActivity implements ValueChangeListener{

    TextView textView;
    TextView textView2;
    TinyCustomDB customDB;

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.sampleTV);
        textView2 = findViewById(R.id.sampleTV2);

        customDB = TinyDB.getInstance().getCustomDatabase(this, "MyCustomDBName");

        customDB.setValueChangeListener(this); // this will trigger when a value will be modified ot deleted
        customDB.putInt("abc",new Random().nextInt(67));
        textView2.setText(customDB.getInt("abc",1)+ "");
    }

    @Override
    public <E> void onValueAdded(String key, E value, String dbName) {
    }

    @Override
    public void onKeyRemoved(String key, String dbName) {
    }

    @Override
    public void onAllKeysRemoved(String dbName) {
    }
}

You can also view the sample app on how it works

Implementation

It is a very simple library and easy to use too. But, not only that, it is also very easy to implement.

  1. Add this line to your settings.gradle file:
maven { url 'https://jitpack.io'}

You can also view the sample settings.gradle file from here.

  1. Add this line to your app level build.gradle add this line:
implementation 'com.github.sambhav2358:TinyDB:2.0.1'

Why do I use this instead of Shared Preferences?

TinyDB eases the process of storing data locally on the device in any format. This is otherwise a very hard work with Shared Prefrences.

//With the library
TinyDefaultDB db = TinyDB.getInstance().getDefaultDatabase(this);
db.putInt("abc",new Random().nextInt(10000));
//With Shred Preferences
SharedPreferences prefs = context.getSharedPreferences("MyAppData",Context.MODE_PRIVATE );
SharedPreferences.Editor editor = prefs.edit();
editor.putString( key, value );
editor.apply();

Usage

In your class you can create the object like this

TinyDefaultDB tinyDB;

Then you can initialize it anywhere and then use it.

Context context = this;// provide the context here.

tinyDB = TinyDB.getInstance().getDefaultDatabase(this);

Saving Data

You can just call one method provide the key and then the value. The rest happens by itself in the background.

This could be an example usage of saving data:

tinyDB = TinyDB.getInstance().getDefaultDatabase(this);
tinyDB.putInt("abc",new Random().nextInt(10000));

You can also view the activity file here.

Retriving data

textView = findViewById(R.id.sampleTV);
tinyDB = TinyDB.getInstance().getDefaultDatabase(this);
textView.setText(tinyDB.getInt("abc",1) + "");

Deleting a key of data

tinyDB.clearKey(String yourKey);

Deleting all the values

tinyDB.clearAll();

Knowing when a value was added, deleted, or all values are deleted

You have listener for data change, removal and etc...

See this:

public class MainActivity extends AppCompatActivity implements ValueChangeListener{

    TextView textView;
    TinyDefaultDB defaultDB;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        defaultDB = TinyDB.getInstance().getDefaultDatabase(this);
        defaultDB.setValueChangeListener(this); // this will trigger when a value will be modified ot deleted
    }

    @Override
    public <E> void onValueAdded(String key, E value, String dbName) {

    }

    @Override
    public void onKeyRemoved(String key, String dbName) {

    }

    @Override
    public void onAllKeysRemoved(String dbName) {

    }
}

Possible types

You can save these kind of formats to save

  1. Integer
  2. Boolean
  3. String
  4. Float
  5. List
  6. Use put and get to get a custom value for other types of formats.

Thanks to

Paper

Feature request

For everyone who wants me to add a feature, just create an issue with the feature you want.

License

Free to use and modify the code.

Author

Build with ❤️ by Sambhav Khandelwal

Stack overflow