MySQL Library Reference
MySQL is a widely‑used, open‑source, client‑server relational database management system. It powers countless web applications, APIs, and data stores, offering:
- Scalability: From small projects to enterprise workloads.
- Client‑server model: Multiple clients can connect over the network.
- Rich SQL support: Joins, transactions, stored procedures, indexes, and more.
- Ecosystem integration: Works with most programming languages and ORMs.
EasyBite includes built‑in MySQL support so you can:
- Connect directly to your MySQL server from scripts.
- Run queries and fetch results as EasyBite arrays or dictionaries.
- Manage transactions (
BEGIN
,COMMIT
,ROLLBACK
). - Reuse connection state (global last‑insert‑id, connection pooling under the hood).
No external plumbing—just import mysql
or from mysql import ...
and you’re ready.
Available Functions
Function | Parameters | Description |
---|---|---|
connect(url) | url (String) | Opens a new connection pool to MySQL. URL format: mysql://user:pass@host:port/dbname . |
create(dbname) | dbname (String) | Creates database dbname if it doesn’t exist on the last connection. |
query(conn, sql) | conn (Connection), sql (String) | Executes SQL. DDL/DML returns true if rows affected > 0; SELECT returns a MySQLResult . |
insertid() | — | Returns last insert ID on the last connection (no args). |
numrows(result) | result (MySQLResult) | Returns number of rows remaining in result . |
fetchall(result) | result (MySQLResult) | Converts entire result set into an array of dictionaries (one per row). |
fetchassoc(result) | result (MySQLResult) | Removes and returns next row as a dictionary, or null if none left. |
fetchone(result) | result (MySQLResult) | Equivalent to fetchrow(result, 0) . |
fetchrow(result, index) | result (MySQLResult), index (Number) | Removes and returns row at index as a numeric array, or error if out of bounds. |
fetcharray(result, mode) | result (MySQLResult), mode (0/1/2) | Removes and returns next row in three modes: 0 =assoc dict, 1 =numeric array, 2 =both combined. |
begin_transaction(conn) | conn (Connection) | Begins a transaction on conn . |
commit(conn) | conn (Connection) | Commits current transaction on conn . |
rollback(conn) | conn (Connection) | Rolls back current transaction on conn . |
escape_string(input) | input (String) | Returns a safely‑escaped version of input for inclusion in SQL literals. |
error() | — | Returns the last error message (currently always "" ). |
version(conn) | conn (Connection) | Returns MySQL server version as a string. |
close(conn) | conn (Connection) | Closes the given connection. |
Examples
All examples begin with importing the module. Use set
to assign values.
import mysql
1. connect(url)
/ create(dbname)
set conn to mysql.connect("mysql://root:pw@localhost:3306/testdb")
show(conn)
// Output:
// "function" // a MySQLConnection object
2. query(conn, sql)
with DDL/DML
set ok1 to mysql.query(conn, "CREATE TABLE IF NOT EXISTS users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50))")
show(ok1)
// Output:
// true
set ok2 to mysql.query(conn, "INSERT INTO users (name) VALUES ('Alice')")
show(ok2)
// Output:
// true
3. insertid()
set lastId to mysql.insertid()
show(lastId)
// Output (example):
// 1
4. Transactions
set _ to mysql.begin_transaction(conn)
set ok to mysql.query(conn, "INSERT INTO users (name) VALUES ('Bob')")
show(ok)
// Output:
// true
set _ to mysql.rollback(conn)
// Bob was not added
set _ to mysql.begin_transaction(conn)
set ok2 to mysql.query(conn, "INSERT INTO users (name) VALUES ('Carol')")
show(ok2)
// Output:
// true
set _ to mysql.commit(conn)
5. query(conn, sql)
with SELECT
set r to mysql.query(conn, "SELECT id, name FROM users")
show(r)
// Output:
// a MySQLResult object
6. fetchall(result)
set allRows to mysql.fetchall(r)
show(allRows)
// Output (example):
// [{"id":1,"name":"Alice"},{"id":3,"name":"Carol"}]
7. numrows(result)
set count to mysql.numrows(r)
show(count)
// Output (example):
// 2
8. fetchassoc(result)
set row1 to mysql.fetchassoc(r)
show(row1)
// Output (example):
// {"id":1,"name":"Alice"}
set row2 to mysql.fetchassoc(r)
show(row2)
// Output (example):
// {"id":3,"name":"Carol"}
set noneLeft to mysql.fetchassoc(r)
show(noneLeft)
// Output:
// null
9. fetchone(result)
set r2 to mysql.query(conn, "SELECT name FROM users")
set first to mysql.fetchone(r2)
show(first)
// Output (example):
// ["Alice"]
10. fetchrow(result, index)
set r3 to mysql.query(conn, "SELECT id FROM users")
set secondId to mysql.fetchrow(r3, 1)
show(secondId)
// Output (example):
// [3]
11. fetcharray(result, mode)
set r4 to mysql.query(conn, "SELECT id, name FROM users")
set asDict to mysql.fetcharray(r4, 0)
show(asDict)
// Output:
// {"id":1,"name":"Alice"}
set asNum to mysql.fetcharray(r4, 1)
show(asNum)
// Output:
// [1,"Alice"]
set asBoth to mysql.fetcharray(r4, 2)
show(asBoth)
// Output:
// {"0":1,"1":"Alice","id":1,"name":"Alice"}
12. escape_string(input)
set raw to "O'Reilly"
set safe to mysql.escape_string(raw)
show(safe)
// Output:
// O''Reilly
13. error()
set err to mysql.error()
show(err)
// Output:
// ""
14. version(conn)
set ver to mysql.version(conn)
show(ver)
// Output (example):
// "8.0.26"
15. close(conn)
set _ to mysql.close(conn)
show(conn)
// Output:
// null // connection no longer valid
Conclusion
With the mysql
library in EasyBite you can:
- Connect to any MySQL server:
mysql.connect(url)
- Run queries and process results as EasyBite arrays/dicts.
- Manage transactions and retrieve last‑insert‑ids seamlessly.
- Escape user input safely for SQL literals.
Just import mysql
or selectively from mysql import connect, query, fetchall
, and you’re ready to build data‑driven scripts and applications in EasyBite.