/jqlite

A Java Library for SQLite

Primary LanguageJavaMIT LicenseMIT

JQLite - An Easy SQLite Java Wrapper


Note: This library has been successfully used in Discord Bots and Minecraft mods.

From V1.x, Version 1.0.4 is the only properly working version.


What is this library?

This library is intended to make working with SQLite databases in java projects easier. It provides some easy to use features which could save you a lot of development time.

How do I use this library?

To use this library, you first need to add it.

Add the following to the repositories block of your build.gradle file:

// For latest releases
maven { url 'https://maven.firstdarkdev.xyz/releases' }

// For Snapshots/Betas
maven { url 'https://maven.firstdarkdev.xyz/snapshots' }

 

Then add the following to dependencies:

// Replace VERSION with one from above
implementation 'me.hypherionmc.jqlite:JQLite:VERSION'

No need for any other dependencies as they are included in the library.


Getting Started

To get started, you first need to create a new JQLite instance.

// Replace testdb with your database name
private final SQLiteDatabase database = new SQLiteDatabase("testdb");

Next, you need to create a "Table Class".

public class MyTableName extends SQLiteTable {

    // This is the primary, autoincrementing key; 
    // THIS MUST ALWAYS BE AT THE TOP ABOVE EVERYTHING ELSE!!!
    @SQLColumn(SQLColumn.Type.PRIMARY)
    private int id;

    @SQLColumn(value = SQLColumn.Type.VARCHAR, maxSize = 255)
    private String name; // A basic VARCHAR column limited to 255 characters

    @SQLColumn(SQLColumn.Type.BOOLEAN)
    private boolean isRegistered; // A basic Boolean column

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setRegistered(boolean registered) {
        isRegistered = registered;
    }

    public boolean isRegistered() {
        return isRegistered;
    }

    public int getId() {
        return id;
    }

}

Then you need to register the Table to the Database engine using

// Single Table
database.registerTable(myTableName);

// Multiple Tables
database.registerTable(table1, table2, table3);

Each class extending SQLiteTable comes with the following methods:

  • insert
  • update
  • fetchAll
  • fetch
  • delete
  • insertUnique
  • insertOrUpdate

Examples

Basic insert example

MyTableName myTable = new MyTableName();
database.registerTable(myTable); // This only needs to be done once!
myTable.setName("John Doe");
myTable.setRegistered(true);

myTable.insert(database);

Basic update example

MyTableName myTable = new MyTableName();
myTable.fetch("name = 'John Doe'");
myTable.setRegistered(false);

myTable.update(database);

Basic delete example

MyTableName myTable = new MyTableName();
myTable.fetch("name = 'John Doe'");

myTable.delete(database);

Basic fetchAll example

MyTableName myTable = new MyTableName();
List<MyTableName> tableList = myTable.fetchAll(database);

for (MyTableName myTable1 : tableList) {
    System.out.println(myTable1.getName());
}

Basic fetchAll with filter example

MyTableName myTable = new MyTableName();
List<MyTableName> tableList = myTable.fetchAll(database, "name = 'John Doe'");

for (MyTableName myTable1 : tableList) {
    System.out.println(myTable1.getName());
}

Basic insertUnique example

MyTableName myTable = new MyTableName();
myTable.setName("John Doe");
myTable.setRegistered(true);

// This will return TRUE if the entry was inserted, 
// or false, if a duplicate already exists
myTable.insertUnique(database, "name = 'John Doe'");

Basic insertOrUpdate example

MyTableName myTable = new MyTableName();
myTable.setName("John Doe");
myTable.setRegistered(true);

// If a duplicate entry is found, it will be updated,
// otherwise it will be inserted
myTable.insertOrUpdate(database, "name = 'John Doe'");

If you need help with this library, please join my discord or open an Issue above