PDEK/Databases Kit/SQLite

SQLite Database Kit

To use this kit you need a little knowledge of SQL language.

Obs: all string and char must be inside of single quotes

To use this kit, firstly you need create a global variable named "db" in the main class of your plugin:

static DataBaseManager db;

You needs too generate the data folder/Config folder of your plugin, his kit has a little bug that doesn't allow to create the plugin configs folder. Is ´possible generate this folder with a config file .yml

After this, in your onEnable function you need to initialize the database, for this insert this code in your onEnable()

MainClass.db = new SQLite(this, "SQLite file name");

SQLite like most of databases use tables and columns, so we need make it, this kit offer to you a method very easy to make tables, with this code:

MainClass.db.createTables(tablename, columns);

"Tablename" is the name of your table, and the columns is the columns name and yours arguments, like "VARCHAR(25) PRIMARY KEY NOT NULL UNIQUE" or "BIGINT NOT NULL DEFAULT 0"

In this example i make a table for a auth system:

We need too a method to access this database with other class, after of all, Java is a Object Oriented Programming Language, to make this we can create a function that direct to own database:

public static DataBaseManager getDatabase() {
       return db;
}

With this, your database file has done and ready, and let's go see the other functions of this kit:

 

runCustomSQL(String sqlCommand)

This function run a custom SQL procedure, but this can't get a value, only set in database

 

replaceInTable(String table, String columns, String data)

This function replace values on a table, if the PRIMARY KEY already exists, and insert a new value if the PRIMARY KEY does't exist

"data" is the new value to set or replace in the columns.

EXAMPLE:

This example use the table "resgistro" create previously, so in this code i make:

Insert the playername "Andrey" in the "PlayerName" column

Insert the password "4654656" in the "password" column

Insert "dasdsa@teste.com" in the "email" column

OBS: All of this value is a VARCHAR, so to get this values, is need use the "getStringTableColumn" function

OBS: IS VERY RECOMMENDED YOU USE THE insertInTables method INSTEAD OF THIS TO INSERT THE FIRSTS VALUES

OBS: IS VERY RECOMMENDED YOU USE THE updateData method INSTEAD OF THIS TO UPDATE AND CHANGE THE DATA THAT ALREADY EXISTS

 

getFloatTableColumn(String primarykey, String columnname, String table)

This function return a Float data

 

getIntTableColumn(String primarykey, String columnname, String table)

This function return a int data

 

getStringTableColumn(String primarykey, String columnname, String table)

This function return a string value

 

getBooleanTableColumn(String primarykey, String columnname, String table)

This function return a boolean value

 

WHERE:

"table" is the name of your table,

"columname" is the name of the column

"Primary key" is the value of the primary key value in the database

 

Testing the database

Have two ways to test your database SQLite:

first is using the SQLiteStudio and using the procedure "SELECT * FROM tablename;"

second is creating a table, inserting values in it, and geting this values and showing in the console, for this you final code is like this:

And on run your plugin with this code, you can see this in server console:

 

                                   CHANGES IN 1.0 ALPHA

Has few changes from the snapshot to the alpha version, the principally change is the adding of two new methods: Update and Insert, and parameters name changed, so let's go see the news two methods:                                         

 

                                                insertInTables Method

It's very recommended you use this method instead of replace method

This method is new of this alpha, and with this you can insert datas in your table, and has two methods to insert datas in your table: inserting in all columns, and inserting in some columns (the column without data of insert is automatically fully with "null");

To insert in all column: you need use the

 insertInTables(String tableName, String values, @Nullable String columnsName)

function, with the columnsName null value, in the tableName you put your table name, and in values you put the values that you need in your column, example: in a table with "playername" "uuid" and "password" columns, you need to use the insertInTables with values separated with comma, and the strings values needs the single quotes, in this example: playername is varchar, uuid is varchar, password is numeric integer:

 insertInTables("registro", "'my playername', 'my uuid', 2145678218", null);

To insert in some columns: is the same thing, you use the same method, but, instead of "null" in columnsName, you need put the columns that you need to use separeted with comma, and in single quotes, for example, i will use the same example, BUT I WILL NOT PUT VALUE IN PASSWORD:

insertInTables("registro", "'my playername', 'my uuid'" , "playername, uuid");

obs: in this case, in the line of "my playername" the password column has the null value

updateData Method

This is the method to update some data of a line in a table, for example: update(change) the password of a player with a command.

The format of this method is:

updateData(String tableName, String columnName, String value, String ConditionColumn, String logicalOperator, String conditionValue)

Where:

  • tableName: is the name of the table you are updating
  • columnName: is the column that you are updating
  • value: the new value of the column name (if string, need in single quotes)
  • conditionColumn: column with a value, it's need to not change all the table, only the line with this condition column
  • logicalOperator: logical operator (=, and, or, < > different of, etc)
  • conditionValue the condition that if is true the update occurs

EXAMPLE:

in the same table of examples of insertInTables, i need change the password of "my playername" to 22222, to make this, i have to be sure that only modify the line of "my playername" to this, the column "player name" must be equals "my playername", so this update would look something like this:

updateData("registro", "password", "222222", "playername", "=" , "'my playername'");

 


Comments

Posts Quoted:
Reply
Clear All Quotes