mysql — MySQL database

Requires: libmysqlclient (auto-installed via minks install mysql)

#include "mysql"

Query results are returned as a JSON string — parse with the Json module.


Method Args Out Description
MySQL:Connect host, user, pass, db Number Connect; returns handle. Throws MySQLException on failure
MySQL:ConnectPort host, port, user, pass, db Number Connect with explicit port
MySQL:Close handle void Close the connection
MySQL:Exec handle, sql Boolean Run DDL / DML; true = success
MySQL:Query handle, sql String SELECT → JSON array of row objects
MySQL:QueryOne handle, sql String SELECT → first row as JSON object, or ""
MySQL:RowsAffected handle Number Rows changed by last Exec
MySQL:LastInsertId handle Number Auto-increment ID of last INSERT
MySQL:Error handle String Last error message, "" if none

Throws MySQLException (with .code = MySQL errno) on connection failure. See Object — Exception.

Var:Number db;
Var:Boolean ok;
Var:String rows;

try {
    MySQL:Connect("localhost", "root", "secret", "mydb", db);
} catch (MySQLException e) {
    Konsol:Print("connect failed (${e.code}): ${e.message}");
}

MySQL:Exec(db, "INSERT INTO users (name) VALUES ('Alice')", ok);

Var:Number lastId;
MySQL:LastInsertId(db, lastId);

MySQL:Query(db, "SELECT * FROM users ORDER BY id", rows);
Konsol:Print(rows);   // [{"id":1,"name":"Alice"}]

MySQL:Close(db);

Building from source

MSYS2 uses MariaDB (libmariadbclient), which provides MySQL-compatible headers.

Platform Command
Debian/Ubuntu sudo apt install libmysqlclient-dev
Fedora/RHEL sudo dnf install mysql-devel
macOS brew install mysql-client
MSYS2 MINGW64 pacman -S mingw-w64-x86_64-libmariadbclient
MSYS2 UCRT64 pacman -S mingw-w64-ucrt-x86_64-libmariadbclient
MSYS2 CLANG64 pacman -S mingw-w64-clang-x86_64-libmariadbclient
MSYS2 CLANGARM64 pacman -S mingw-w64-clang-aarch64-libmariadbclient

For native Windows, download and install MySQL Connector/C. Override MYSQL_DIR if your install path differs from the default:

make -C mysql_plugin MYSQL_DIR="C:/your/install/path"

Examples

// test_mysql.ks — MySQL plugin tests
// Run: minks --plugin ./kse_mysql.dll test_mysql.ks
// Requires a running MySQL server. Adjust the connection details below.

Konsol:Print("=== MySQL plugin ===");

Var:Number db;
Var:Boolean ok;
Var:String  rows;
Var:String  row;
Var:String  err;
Var:Number  n;

// ── Connect (typed exception on failure) ───────────────────────────────────────

Konsol:Print("--- Connect bad credentials (expect MySQLException)");
try {
    MySQL:Connect("localhost", "root", "wrongpassword", "test", db);
} catch (MySQLException e) {
    Konsol:Print("caught MySQLException : ${e.message}");
    Konsol:Print("error code            : ${e.code}");
} catch (Exception e) {
    Konsol:Print("caught Exception : ${e.message}");
}

Konsol:Print("--- Connect");
MySQL:Connect("localhost", "root", "secret", "test", db);
Konsol:Print("handle : ${db}");

// ── CREATE TABLE ───────────────────────────────────────────────────────────────

Konsol:Print("--- CREATE TABLE");
MySQL:Exec(db, "DROP TABLE IF EXISTS minks_users", ok);
MySQL:Exec(db, "CREATE TABLE minks_users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL, age INT)", ok);
Konsol:Print("ok     : ${ok}");
MySQL:Error(db, err);
Konsol:Print("error  : '${err}'");

// ── INSERT ─────────────────────────────────────────────────────────────────────

Konsol:Print("--- INSERT");
MySQL:Exec(db, "INSERT INTO minks_users (name, age) VALUES ('Alice', 30)", ok);
MySQL:LastInsertId(db, n);
Konsol:Print("ok, last_id : ${ok} ${n}");

MySQL:Exec(db, "INSERT INTO minks_users (name, age) VALUES ('Bob', 25)", ok);
MySQL:LastInsertId(db, n);
Konsol:Print("ok, last_id : ${ok} ${n}");

MySQL:Exec(db, "INSERT INTO minks_users (name, age) VALUES ('Carol', 35)", ok);
MySQL:LastInsertId(db, n);
Konsol:Print("ok, last_id : ${ok} ${n}");

// ── SELECT all ─────────────────────────────────────────────────────────────────

Konsol:Print("--- SELECT all");
MySQL:Query(db, "SELECT * FROM minks_users ORDER BY id", rows);
Konsol:Print("rows   : ${rows}");

// ── SELECT filtered ────────────────────────────────────────────────────────────

Konsol:Print("--- SELECT WHERE age > 28");
MySQL:Query(db, "SELECT * FROM minks_users WHERE age > 28 ORDER BY age", rows);
Konsol:Print("rows   : ${rows}");

// ── QueryOne ───────────────────────────────────────────────────────────────────

Konsol:Print("--- QueryOne");
MySQL:QueryOne(db, "SELECT * FROM minks_users WHERE name = 'Bob'", row);
Konsol:Print("row    : ${row}");

MySQL:QueryOne(db, "SELECT * FROM minks_users WHERE name = 'Nobody'", row);
Konsol:Print("no match (expect empty) : '${row}'");

// ── UPDATE ─────────────────────────────────────────────────────────────────────

Konsol:Print("--- UPDATE");
MySQL:Exec(db, "UPDATE minks_users SET age = 31 WHERE name = 'Alice'", ok);
MySQL:RowsAffected(db, n);
Konsol:Print("ok, rows_affected : ${ok} ${n}");

MySQL:QueryOne(db, "SELECT age FROM minks_users WHERE name = 'Alice'", row);
Konsol:Print("alice age now : ${row}");

// ── DELETE ─────────────────────────────────────────────────────────────────────

Konsol:Print("--- DELETE");
MySQL:Exec(db, "DELETE FROM minks_users WHERE name = 'Carol'", ok);
MySQL:RowsAffected(db, n);
Konsol:Print("ok, rows_affected : ${ok} ${n}");

MySQL:Query(db, "SELECT * FROM minks_users ORDER BY id", rows);
Konsol:Print("remaining : ${rows}");

// ── Error handling ─────────────────────────────────────────────────────────────

Konsol:Print("--- bad SQL");
MySQL:Exec(db, "THIS IS NOT SQL", ok);
Konsol:Print("ok (expect false) : ${ok}");
MySQL:Error(db, err);
Konsol:Print("error  : '${err}'");

// ── ConnectPort (non-default port) ─────────────────────────────────────────────

Konsol:Print("--- ConnectPort");
Var:Number db2;
MySQL:ConnectPort("localhost", 3306, "root", "secret", "test", db2);
Konsol:Print("handle : ${db2}");
MySQL:Query(db2, "SELECT COUNT(*) AS total FROM minks_users", rows);
Konsol:Print("count  : ${rows}");
MySQL:Close(db2);

// ── Cleanup + Close ────────────────────────────────────────────────────────────

Konsol:Print("--- Cleanup");
MySQL:Exec(db, "DROP TABLE IF EXISTS minks_users", ok);
MySQL:Close(db);
Konsol:Print("closed");

Konsol:Print("=== done ===");