Nox
Download

Standard library

nox.mysql

A MySQL / MariaDB driver over libmysqlclient (or the binary-compatible MariaDB connector), loaded at run time on the first open_url. A missing library raises a clean MysqlError.

Text
import nox.mysql
from nox.mysql import Connection, MysqlError

Capability: network.

Functions and methods#

Member Description
open_url(conn_url) connects using mysql://user:pass@host:port/database (the port defaults to 3306); raises MysqlError on failure
Connection.execute(sql) runs a statement; returns the affected row count
Connection.query(sql) runs a query; returns list[Row]
Connection.prepare(sql) a parameterised Statement with ? placeholders
Connection.last_insert_rowid() the auto-increment id of the last INSERT
Connection.changes() rows changed by the last statement
Connection.close() closes the connection

Parameters are bound by zero-based index. This driver escapes string values on the client with the library's own mysql_real_escape_string and substitutes them into the statement, so bound values are protected from SQL injection even though there is no server-side prepared statement.

Nox
import nox.mysql
from nox.mysql import Connection, MysqlError
from nox.db import Row, Statement

def add_user(conn: Connection, name: str) -> int:
    st: Statement = conn.prepare("INSERT INTO users (name) VALUES (?)")
    st.bind_str(0, name)
    st.execute()
    return conn.last_insert_rowid()

def connect() -> Connection:
    return nox.mysql.open_url("mysql://app:secret@localhost:3306/appdb")

Notes#

  • Install libmysqlclient (or MariaDB Connector/C) on machines that connect.
  • Do not build SQL by concatenating untrusted text; use bind_*.
  • Do not share a connection between tasks that may run on different workers.

On a mobile target (iOS, Android)#

There is no libmysqlclient on a phone, so on ios-* and android-* targets nox.mysql uses a MySQL/MariaDB client written in Zig, over the operating system's sockets. The API is the same; nothing has to be installed. Authentication: mysql_native_password and caching_sha2_password (fast path, and the full path with the server's RSA key, so the password never travels in plain text even without TLS), including an authentication plugin switch. Queries use the text protocol (COM_QUERY); a result set is held in memory once the reply is complete, and only the first of several result sets is returned.

  • There is no TLS yet. The password is protected, but queries and results are not: only connect to a server over a network you trust (loopback, a VPN, a private network). A phone app should normally talk to an HTTP API rather than straight to a database.
  • host is always a TCP host (no Unix socket for localhost). Calls block the calling thread, and there is no read timeout.
  • Verified on the iOS simulator and the Android emulator against a real MySQL 8.4 server (scripts/mobile_db_probe.py).