The getTable()
and createTable()
functions let you save either an existing table or a newly created table into a variable.
Syntax
Use the getTable()
method to fetch a table from the mariadb
database:
package org.example;
import mindsdb.MindsDB;
import mindsdb.models.Database;
import mindsdb.models.MDBTable;
import mindsdb.services.Server;
public class Main {
public static void main(String[] args) {
Server server = MindsDB.connect();
Database mariadb = server.getDatabase("mariadb");
MDBTable testTable = mariadb.getTable("test");
}
}
Or, use the createTable()
method to create a new table:
/**
* Create a table from a query
*
* @param name - Name of the table
* @param df - Table to create the table from
* @param replace - Replace the table if it already exists
* @return Table
*/
public MDBTable createTable(String name, tech.tablesaw.api.Table df, boolean replace);
/**
* Create a table from a query
*
* @param name - Name of the table
* @param query - Query to create the table from
* @param replace - Replace the table if it already exists
* @return Table
*/
public MDBTable createTable(String name, Query query, boolean replace);
Example: Creating table using another table
Database mariadb = server.getDatabase("mariadb");
Database exampleDb = server.getDatabase("example_db");
MDBTable testTable = mariadb.createTable("another_table", exampleDb.query("SELECT * FROM home_rentals LIMIT 10;"), true);
Only files database can support creating table using Tablesaw Table:
Database filesDB = server.getDatabase("files");
Table userTable = Table.create()
.addColumns(StringColumn.create("Name", "Alice", "Bob", "Charlie"))
.addColumns(IntColumn.create("age", 25, 30, 35))
.addColumns(DoubleColumn.create("height", 1.75, 1.80, 1.85));
MDBTable user = mariadb.createTable("user_table", userTable, true);