- Compatible API Versions
- 2.0.0
- Contributors
- Ragnok123
Simple SQL library for Cloudburst server
SqlNukkitLib is asynchronous library plugin, which contains sql drivers and which can be used as a plugin for your server and also as a library in your external java programs.
CONNECTIONS:
Currently, library supports only two types of database connections:
- MySQL
- SQLite3
Java:
Database db = SQLLib.init(SQLType type, SQLConnectionInfo info);
For mysql, SQLConnection info is like this:
Java:
info = new MySQLConnectionInfo(String ip, String user, String password, String database, String port);
Java:
info = new SQLite3ConnectionInfo(new File(path_to_db));
Java:
db.connect();
db.close();
To make life easier for developers, sqlNukkitLib contains built-in methods to make sql queries more quicker and easier.
First and very basic method is db.query();
Java:
db.query("INSERT INTO `players` (`nickname`,`money`) VALUES ('ragnok123','0');");
To insert data, use function db.insert();. Example:
Java:
db.insert("players", new Pair[]{
new Pair("nickname", "Ragnok"), new Pair("money", 0.0), new Pair("rank","User")
});
Java:
db.update("players", "nickname", "Ragnok", new Pair[]{
new Pair("money", 100.0), new Pair("rank", "VIP")
});
Note, that select method does return void with HashMap in Consumer instead of HashMap object.
Java:
db.select("players", "nickname", "Ragnok", map -> {
if(map.isEmpty()){
getLogger().info("This player doesn't exists");
} else {
double money = (double)map.get("money");
String rank = (String)map.get("rank");
getLogger().info("Player Ragnok information:");
getLogger().info(">> Money: " + String.valueOf(money));
getLogger().info(">> Rank: " + rank);
}
});