SQLib is the easiest way to store data for all your minecraft needs! Its simple fabric based sql wrapper made with a focus on minecraft use cases.
This library is not a full fledged sql wrapper, and does not provide full access to many sql features. The main focus of this library is to provide an easy and simple way to store data in your mods. If you are looking for a more advanced database I recommend taking a look at something like Nitrite.
The library first needs to be included into your project. The simplest way to do this is through Jitpack. In your build.gradle make sure to include:
repositories {
maven { url 'https://jitpack.io' }
}
dependencies {
modImplementation 'com.github.MrNavaStar:SQLib:v1.2.0'
//Or if you wish to include with the mod:
include(modImplementation('com.github.MrNavaStar:SQLib:v1.2.0'))
}
The database must first be setup to use either sqlite or mysql like so:
- SQLITE
Database database = new SQLiteDatabase("name", "/awesome/dir");
- MYSQL
Database database = new MySQLDatabase("name", "127.0.0.1", "3306", "username", "password");
Now a table can be added to store data. This table will automatically be added to the database (Note that it is not required to include your mod id in the table name, however doing so will result in less conflict with other mods that name their tables the same):
Table playerData = database.createTable(MODID + " name");
In order to store data in the table you need a DataContainer object. This will simply provide a way to handle your data in an object orientied manner.
DataContainer player = playerData.createDataContainer(id);
Finally, you can use the DataContainer to put, get, and drop data:
player.put("NickName", "Bob Ross");
player.put("Health", 100);
String name = player.getString("NickName");
String health = player.getInt("Health");
player.dropString("NickName");
player.dropInt("Health");