libstd/mysql

Bindings to the MySQL/MariaDB client library plus helpers to connect, run statements, read result sets and escape values. On Linux it is libmariadb.so; on Windows libmariadb.dll, from MariaDB Connector/C or the lib folder of a MariaDB installation. It is not a system DLL: its folder must be on PATH to compile and to run the program.

#include <libstd/mysql>

Includes libstd/stdio. Links the shared library libmariadb.so, libmariadb.dll (FFI).

Example

#include <libstd/mysql>

conn = mysql_connect_or_die("127.0.0.1", "root", "", 0 as str, MYSQL_DEFAULT_PORT)
mysql_query_w(conn, "CREATE DATABASE IF NOT EXISTS demo")
mysql_query_w(conn, "USE demo")
mysql_query_w(conn, "CREATE TABLE IF NOT EXISTS Users(id INT, nome VARCHAR(50))")
mysql_query_w(conn, "INSERT INTO Users VALUES(1, '" + mysql_escape(conn, "Al'ice") + "')")

res = mysql_query_result(conn, "SELECT id, nome FROM Users ORDER BY id")
mysql_print_result(res)                  // one row per line, fields separated by " | "
mysql_free_result(res)
mysql_close(conn)

Constants

NameValueMeaning
MYSQL_DEFAULT_PORT3306

Functions

extern def mysql_init(mysql: MYSQL) -> MYSQL

Allocates a connection handle.

extern def mysql_real_connect(mysql: MYSQL, host: str, user: str, passwd: str, db: str, port: int, socket: str, flags: long) -> MYSQL

Connects to a server.

extern def mysql_close(mysql: MYSQL) -> None

Closes a connection.

extern def mysql_ping(mysql: MYSQL) -> int

Checks that the connection is alive.

extern def mysql_get_client_info() -> str

Client library version.

extern def mysql_get_server_info(mysql: MYSQL) -> str

Server version.

extern def mysql_query(mysql: MYSQL, query: str) -> int

Runs an SQL statement; 0 on success.

extern def mysql_error(mysql: MYSQL) -> str

Last error message.

extern def mysql_errno(mysql: MYSQL) -> int

Last error code.

extern def mysql_affected_rows(mysql: MYSQL) -> long

Rows affected by the last statement.

extern def mysql_insert_id(mysql: MYSQL) -> long

Auto-increment id of the last insert.

extern def mysql_real_escape_string(mysql: MYSQL, to: str, src: str, length: long) -> long

Escapes a string for use inside a query.

extern def mysql_store_result(mysql: MYSQL) -> MYSQL_RES

Fetches the whole result set to the client.

extern def mysql_use_result(mysql: MYSQL) -> MYSQL_RES

Streams the result set row by row.

extern def mysql_free_result(result: MYSQL_RES) -> None

Frees a result set.

extern def mysql_num_rows(result: MYSQL_RES) -> long

Rows in a stored result.

extern def mysql_num_fields(result: MYSQL_RES) -> int

Columns in a result.

extern def mysql_field_count(mysql: MYSQL) -> int

Columns of the last statement.

extern def mysql_fetch_row(result: MYSQL_RES) -> str

The next row (an array of C strings), or null.

extern def mysql_fetch_lengths(result: MYSQL_RES) -> str

Lengths of the fields of the current row.

extern def mysql_fetch_field(result: MYSQL_RES) -> str

The next column definition.

def mysql_checker_error(mysql: MYSQL, response: int) -> int

If response is not 0, prints mysql_error, closes the connection and exits.

def mysql_connect_or_die(host: str, user: str, passwd: str, db: str, port: int) -> MYSQL
mysql_init + mysql_real_connect, exiting with a clear message on failure. Pass 0 as str as db to select none.
def mysql_query_w(mysql: MYSQL, query: str) -> int

Runs a statement without a result set, with automatic error checking.

def mysql_query_result(mysql: MYSQL, query: str) -> MYSQL_RES

Runs a SELECT and returns its stored result set (all rows fetched to the client).

def mysql_row_is_null(row: str, index: int) -> int

1 if field index of a row is SQL NULL.

def mysql_row_get(row: str, index: int) -> str

Field index of a row as a string (NULL becomes "").

def mysql_field_name(result: MYSQL_RES) -> str

Name of the next column (the library advances an internal cursor): call it mysql_num_fields times in a row.

def mysql_escape(mysql: MYSQL, value: str) -> str

A copy of value with SQL special characters escaped, ready to be concatenated into a query.

def mysql_print_result(result: MYSQL_RES) -> None

Prints a whole result set, one row per line.