SQLibrary

This project is abandoned and its default file will likely not work with the most recent version of Minecraft. Whether this project is out of date or its author has marked it as abandoned, this project is no longer maintained.

SQLibrary 1.0.0 Beta:

A Bukkit / Spigot developer's solution to SQL

SQLibrary is a SQL Library with many many tools to connect to / creating SQL databases and files.With easy an easy to use connection API and QueryBuilder

SQLibrary on GitHub

Implementing SQLibrary dependency to your plugin

onEnable Function

if(!Bukkit.getPluginManager().isPluginEnabled("SQLibrary")) {
	Bukkit.getLogger().log(
		Level.SEVERE,
		"Plugin was unable to start | SQLibrary is not installed"
	);
			
	this.setEnabled(false);
	return;
}

Add to plugin.yml

soft-depend: [SQLibrary]

Connecting to a SQL Database

Connecting via MySQL

SQLDatabase database = null;
try {
	database = new MySQL("hostname", "port", "database", "username", "password");
        database.openConnection();
}
catch (SQLException | ClassNotFoundException e) {
	Bukkit.getLogger.log(Level.SEVERE, "Unable to connect to MySQL database", e);
	return;
}

Connecting via SQLite

SQLDatabase database = null;
try {
	database = new SQLite(new File("path to database file"));
        database.openConnection();
}
catch (SQLException | ClassNotFoundException e) {
	Bukkit.getLogger.log(Level.SEVERE, "Unable to connect to SQLite database", e);
	return;
}

QueryBuilder Examples

SQL Table Builder

In this example we will be creating a player statistics table that holds player kills, deaths and gold

/* Create a new TableBuilder */
TableBuilder builder = QueryBuilder.createTable("player_stats");
builder.createIfNotExists();

/* Create a new Column Builder |  new ColumnBuilder(String columnName, ColumnType type, new ColumnMeta(size)) */
ColumnBuilder columnBuilder = new ColumnBuilder("player_id", ColumnType.VARCHAR, new ColumnMeta(25).primaryKey().notNull());

/* Append additional columns to the Column Builder */
columnBuilder.appendColumn("player_kills", ColumnType.INT, new ColumnMeta(10).notNull().defaultValue(0));
columnBuilder.appendColumn("player_deaths", ColumnType.INT, new ColumnMeta(10).notNull().defaultValue(0));
columnBuilder.appendColumn("player_gold", ColumnType.INT, new ColumnMeta(10).notNull().defaultValue(500));

/* Set the Column Builder */
builder.columns(columnBuilder);

/* Execute the SQL Update */
database.executeUpdate(builder.build());

SQL Select Builder

/* Create a new Select Builder */
SelectBuilder selectBuilder = QueryBuilder.selectAll("player_stats");
/* OR */
SelectBuilder selectBuilder = QueryBuilder.select("player_stats", {"player_kills", "player_deaths", "player_gold"});

/* Create a new Condition Builder to add to our SelectBuilder */
ConditionBuilder conditionBuilder = new ConditionBuillder(Condition.fieldEquals("player_id", "SOME PLAYER ID")
selectBuilder.where(conditionBuilder);

/* Execute the query and retrieve a Result Set */
ResultSet set = database.executeQuery(selectBuilder.build());

/* Process the Result set */
if(set.next) {
	/* Process Player Data */
}
else {
	/* No player data in database */
}

SQL Update Builder

/* Create our UpdateBuilder */
UpdateBuilder builder = QueryBuilder.update("player_stats");

/* Set the Values we wish to update, In this example I am going to update the player kills by +1 and set the player gold to 2000 */
builder.values(new Value[] {
	
	/* SQLibrary will automatically format " ' " around Strings. Casting a String to a Object will allow you to set a un-formatted value */
	new Value("player_kills", (Object) "`player_kills` + 1");
	new Value("player_gold", 1000);

});

/* Create a new Condition Builder */
ConditionBuilder conditionBuilder = new ConditionBuilder(Condition.fieldEquals("player_id", "SOME PLAYER ID"));

/* Add another condition to the builder for an example */
conditionBuilder.and(Condition.fieldEqualOrLessThan("player_gold", 1000));

/* Add the ConditionBuilder and Execute the Update */
builder.where(conditionBuilder);
database.executeUpdate(builder.build());

SQL Insert Builder

/* Create our InsertBuilder */
InsertBuilder builder = QueryBuilder.insertInto("player_stats");

/* Set the Values we want to insert */
builder.values(new Value[] {
	new Value("player_id", "SOME PLAYER ID");
	new Value("player_kills", 12);
	new Value("player_deaths", 0);
	new Value("player_gold", 6000);
});

/* Execute the Update */
database.executeUpdate(builder.build());

Comments

  • To post a comment, please or register a new account.
Posts Quoted:
Reply
Clear All Quotes

About This Project

  • Project ID
    90821
  • Created
    Apr 3, 2015
  • Last Released File
    Apr 3, 2015
  • Total Downloads
    1,031
  • License

Categories

Members

Recent Files