Skip to content
n-tdi edited this page Feb 18, 2023 · 3 revisions

Table

A table represents a Postgresql table! You're able to create tables and insert rows into them. If you're interested in modifying or accessing a single row in a table, see Row. If you don't know how to use a database object, you should see Database.

Accessing and Creating a table

By default, if a table does not exist within the Database that we made earlier, it will automatically create one with the given information.

Here is an example of how to create a basic table with Postglam.

Main.java

public class Main {
    public static void main(String[] args) {
        Database database = ...
        database.connect();

        String tableName = "mytable"; // Table name
        Map.Entry<String, Datatypes> primaryKey = Map.entry("id", Datatypes.UUID); // Id column of the table

        LinkedHashMap<String, Datatypes> keys = new LinkedHashMap<>(); // Hashmap of the rest of the columns
        keys.put("name", Datatypes.TEXT);
        keys.put("level", Datatypes.INTEGER);
        keys.put("money", Datatypes.INTEGER);

        Table table = new Table(tableName, primaryKey, keys);
    }
}

Wonderful! We've just created our first table. The code is so simple that it should be self explanatory, but I'll explain it anyway. new Table() takes a string for the name of the table, and then a primary key value. This is the value that will be used for finding rows in the table and such. Lastly, we can put as many other columns and their respective datatypes in the table.

If the table already exists in the Postgresql database, it is important that the column labels and datatypes are exactly correct.

Inserting rows into a table

Clone this wiki locally