db/mysql
Connecting to MySQL and MariaDB.
import db.mysql
This package implements the MySQL and MariaDB protocol directly and needs no outside library; when the server offers TLS the connection is encrypted. The example needs a running server, so it is checked and not run. For text outside ASCII, call SetCharset with utf8mb4 once you are connected.
Types
Database
struct Database| Name | Type | Description |
|---|---|---|
handle | void* | The pointer to the connection state. |
import db
import db.mysql
func main:
if not mysql.Available():
println "the MySQL client is not available"
ret
end
_ := mysql.LibraryInit()
_ := mysql.ThreadInit()
println mysql.ClientVersion()
d := mysql.Open("127.0.0.1", "app", "s3cret", "shop", 3306)
if not mysql.Ok(d):
println mysql.Errno(d), mysql.Error(d)
ret
end
println mysql.ServerVersion(d), mysql.Ping(d)
_ := mysql.SetCharset(d, "utf8mb4")
_ := mysql.SelectDB(d, "shop")
name := mysql.Escape(d, "O'Brien")
_ := mysql.Exec(d, "INSERT INTO customers (name) VALUES ('" + name + "')")
println mysql.LastInsertId(d), mysql.AffectedRows(d), mysql.Changes(d)
_ := mysql.Autocommit(d, false)
_ := mysql.Begin(d)
_ := mysql.Exec(d, "UPDATE products SET stock = stock - 1 WHERE id = 7")
if mysql.QueryInt(d, "SELECT stock FROM products WHERE id = 7") < 0:
_ := mysql.Rollback(d)
else:
_ := mysql.Commit(d)
end
mut result := mysql.Query(d, "SELECT id, name, price, note FROM products")
if mysql.QueryOk(result):
println mysql.ColumnCount(result), mysql.RowCount(result), mysql.ColumnName(result, 1)
until mysql.Next(result):
println mysql.Int(result, 0), mysql.Text(result, 1), mysql.Float(result, 2), mysql.Int64(result, 0), mysql.IsNull(result, 3)
end
mysql.Finish(result)
end
println mysql.QueryText(d, "SELECT VERSION()")
c := mysql.Conn(d)
println c.Kind(), db.QueryInt(c, "SELECT COUNT(*) FROM products")
println d.Kind(), d.Ping(), d.Quote("x'y")
rows := d.Query("SELECT name FROM customers")
until rows.Next():
println rows.Text(0)
end
_ := rows.Close()
mysql.Close(d)
mysql.ThreadEnd()
mysql.LibraryEnd()
_ := mysql.OpenFull("localhost", "app", "s3cret", "shop", 0, "/tmp/mysql.sock", 0 as u64)
endKind
func Kind(): strThe example for this item is shown with the ones above: db-mysql-Client.salam
Ok
func Ok(): boolThe example for this item is shown with the ones above: db-mysql-Client.salam
Ping
func Ping(): boolThe example for this item is shown with the ones above: db-mysql-Client.salam
Exec
func Exec(sql: str): intThe example for this item is shown with the ones above: db-mysql-Client.salam
Query
func Query(sql: str): dyn db.RowsThe example for this item is shown with the ones above: db-mysql-Client.salam
Begin
func Begin(): dyn db.TxThe example for this item is shown with the ones above: db-mysql-Client.salam
LastInsertId
func LastInsertId(): i64The example for this item is shown with the ones above: db-mysql-Client.salam
Changes
func Changes(): i64The example for this item is shown with the ones above: db-mysql-Client.salam
Error
func Error(): strThe example for this item is shown with the ones above: db-mysql-Client.salam
Quote
func Quote(s: str): strThe example for this item is shown with the ones above: db-mysql-Client.salam
Close
func Close(): intThe example for this item is shown with the ones above: db-mysql-Client.salam
Result
struct Result| Name | Type | Description |
|---|---|---|
handle | void* | The result pointer. |
row | void* | The current row. |
The example for this item is shown with the ones above: db-mysql-Client.salam
Ok
func Ok(): boolThe example for this item is shown with the ones above: db-mysql-Client.salam
Next
func Next(): boolThe example for this item is shown with the ones above: db-mysql-Client.salam
ColumnCount
func ColumnCount(): intThe example for this item is shown with the ones above: db-mysql-Client.salam
ColumnName
func ColumnName(col: int): strThe example for this item is shown with the ones above: db-mysql-Client.salam
ColumnType
func ColumnType(col: int): intTYPE_... constants in db.The example for this item is shown with the ones above: db-mysql-Client.salam
IsNull
func IsNull(col: int): boolThe example for this item is shown with the ones above: db-mysql-Client.salam
Text
func Text(col: int): strThe example for this item is shown with the ones above: db-mysql-Client.salam
Int
func Int(col: int): intThe example for this item is shown with the ones above: db-mysql-Client.salam
Int64
func Int64(col: int): i64The example for this item is shown with the ones above: db-mysql-Client.salam
Float
func Float(col: int): f64The example for this item is shown with the ones above: db-mysql-Client.salam
Close
func Close(): intThe example for this item is shown with the ones above: db-mysql-Client.salam
Txn
struct Txn| Name | Type | Description |
|---|---|---|
handle | void* | The connection pointer. |
The example for this item is shown with the ones above: db-mysql-Client.salam
Ok
func Ok(): boolThe example for this item is shown with the ones above: db-mysql-Client.salam
Exec
func Exec(sql: str): intThe example for this item is shown with the ones above: db-mysql-Client.salam
Query
func Query(sql: str): dyn db.RowsThe example for this item is shown with the ones above: db-mysql-Client.salam
Commit
func Commit(): intThe example for this item is shown with the ones above: db-mysql-Client.salam
Rollback
func Rollback(): intThe example for this item is shown with the ones above: db-mysql-Client.salam
Functions
Available
func Available(): boolThe example for this item is shown with the ones above: db-mysql-Client.salam
ClientVersion
func ClientVersion(): strThe example for this item is shown with the ones above: db-mysql-Client.salam
ServerVersion
func ServerVersion(db: Database): strThe example for this item is shown with the ones above: db-mysql-Client.salam
Open
func Open(host: str, user: str, pass: str, dbname: str, port: int): DatabaseThe example for this item is shown with the ones above: db-mysql-Client.salam
OpenFull
func OpenFull(host: str, user: str, pass: str, dbname: str, port: int, socket: str, flags: u64): DatabaseThe example for this item is shown with the ones above: db-mysql-Client.salam
Ok
func Ok(db: Database): boolThe example for this item is shown with the ones above: db-mysql-Client.salam
Close
func Close(db: Database)The example for this item is shown with the ones above: db-mysql-Client.salam
Ping
func Ping(db: Database): boolThe example for this item is shown with the ones above: db-mysql-Client.salam
SelectDB
func SelectDB(db: Database, name: str): intThe example for this item is shown with the ones above: db-mysql-Client.salam
SetCharset
func SetCharset(db: Database, charset: str): intutf8mb4 for text outside ASCII.The example for this item is shown with the ones above: db-mysql-Client.salam
Error
func Error(db: Database): strThe example for this item is shown with the ones above: db-mysql-Client.salam
Errno
func Errno(db: Database): intThe example for this item is shown with the ones above: db-mysql-Client.salam
Exec
func Exec(db: Database, sql: str): intThe example for this item is shown with the ones above: db-mysql-Client.salam
AffectedRows
func AffectedRows(db: Database): i64The example for this item is shown with the ones above: db-mysql-Client.salam
Changes
func Changes(db: Database): i64The example for this item is shown with the ones above: db-mysql-Client.salam
LastInsertId
func LastInsertId(db: Database): i64The example for this item is shown with the ones above: db-mysql-Client.salam
Begin
func Begin(db: Database): intThe example for this item is shown with the ones above: db-mysql-Client.salam
Commit
func Commit(db: Database): intThe example for this item is shown with the ones above: db-mysql-Client.salam
Rollback
func Rollback(db: Database): intThe example for this item is shown with the ones above: db-mysql-Client.salam
Autocommit
func Autocommit(db: Database, enabled: bool): intThe example for this item is shown with the ones above: db-mysql-Client.salam
Query
func Query(db: Database, sql: str): ResultThe example for this item is shown with the ones above: db-mysql-Client.salam
QueryOk
func QueryOk(res: Result): boolThe example for this item is shown with the ones above: db-mysql-Client.salam
Next
func Next(res &: Result): boolThe example for this item is shown with the ones above: db-mysql-Client.salam
Finish
func Finish(res: Result)The example for this item is shown with the ones above: db-mysql-Client.salam
ColumnCount
func ColumnCount(res: Result): intThe example for this item is shown with the ones above: db-mysql-Client.salam
RowCount
func RowCount(res: Result): i64The example for this item is shown with the ones above: db-mysql-Client.salam
ColumnName
func ColumnName(res: Result, col: int): strThe example for this item is shown with the ones above: db-mysql-Client.salam
Text
func Text(res: Result, col: int): strThe example for this item is shown with the ones above: db-mysql-Client.salam
Int
func Int(res: Result, col: int): intThe example for this item is shown with the ones above: db-mysql-Client.salam
Int64
func Int64(res: Result, col: int): i64The example for this item is shown with the ones above: db-mysql-Client.salam
Float
func Float(res: Result, col: int): f64The example for this item is shown with the ones above: db-mysql-Client.salam
IsNull
func IsNull(res: Result, col: int): boolThe example for this item is shown with the ones above: db-mysql-Client.salam
Escape
func Escape(db: Database, s: str): strThe example for this item is shown with the ones above: db-mysql-Client.salam
QueryInt
func QueryInt(db: Database, sql: str): intThe example for this item is shown with the ones above: db-mysql-Client.salam
QueryText
func QueryText(db: Database, sql: str): strThe example for this item is shown with the ones above: db-mysql-Client.salam
LibraryInit
func LibraryInit(): boolThe example for this item is shown with the ones above: db-mysql-Client.salam
LibraryEnd
func LibraryEnd()The example for this item is shown with the ones above: db-mysql-Client.salam
ThreadInit
func ThreadInit(): boolThe example for this item is shown with the ones above: db-mysql-Client.salam
ThreadEnd
func ThreadEnd()The example for this item is shown with the ones above: db-mysql-Client.salam
Conn
func Conn(d: Database): dyn db.ConnectionConnection.The example for this item is shown with the ones above: db-mysql-Client.salam