std/db_mysql

    Dark Mode
Search:
  Source   Edit

A higher level mySQL database wrapper. The same interface is implemented for other databases too.

See also: db_odbc, db_sqlite, db_postgres.

Parameter substitution

All db_* modules support the same form of parameter substitution. That is, using the ? (question mark) to signify the place where a value should be placed. For example:

sql"INSERT INTO myTable (colA, colB, colC) VALUES (?, ?, ?)"

Examples

Opening a connection to a database

import std/db_mysql
let db = open("localhost", "user", "password", "dbname")
db.close()

Creating a table

db.exec(sql"DROP TABLE IF EXISTS myTable")
db.exec(sql("""CREATE TABLE myTable (
                 id integer,
                 name varchar(50) not null)"""))

Inserting data

db.exec(sql"INSERT INTO myTable (id, name) VALUES (0, ?)",
        "Dominik")

Larger example

import std/[db_mysql, math]

let theDb = open("localhost", "nim", "nim", "test")

theDb.exec(sql"Drop table if exists myTestTbl")
theDb.exec(sql("create table myTestTbl (" &
    " Id    INT(11)     NOT NULL AUTO_INCREMENT PRIMARY KEY, " &
    " Name  VARCHAR(50) NOT NULL, " &
    " i     INT(11), " &
    " f     DECIMAL(18,10))"))

theDb.exec(sql"START TRANSACTION")
for i in 1..1000:
  theDb.exec(sql"INSERT INTO myTestTbl (name,i,f) VALUES (?,?,?)",
        "Item#" & $i, i, sqrt(i.float))
theDb.exec(sql"COMMIT")

for x in theDb.fastRows(sql"select * from myTestTbl"):
  echo x

let id = theDb.tryInsertId(sql"INSERT INTO myTestTbl (name,i,f) VALUES (?,?,?)",
        "Item#1001", 1001, sqrt(1001.0))
echo "Inserted item: ", theDb.getValue(sql"SELECT name FROM myTestTbl WHERE id=?", id)

theDb.close()

Types

DbConn = distinct PMySQL
encapsulates a database connection   Source   Edit
InstantRow = object
  row: cstringArray
  len: int
a handle that can be used to get a row's column text on demand   Source   Edit
Row = seq[string]
a row of a dataset. NULL database values will be converted to nil.   Source   Edit

Procs

proc `[]`(row: InstantRow; col: int): string {.inline, ...raises: [], tags: [].}
Returns text for given column of the row.   Source   Edit
proc close(db: DbConn) {....tags: [DbEffect], raises: [].}
closes the database connection.   Source   Edit
proc dbError(db: DbConn) {.noreturn, ...raises: [DbError], tags: [].}
raises a DbError exception.   Source   Edit
proc dbQuote(s: string): string {....raises: [], tags: [].}
DB quotes the string.   Source   Edit
proc exec(db: DbConn; query: SqlQuery; args: varargs[string, `$`]) {.
    ...tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].}
executes the query and raises EDB if not successful.   Source   Edit
proc execAffectedRows(db: DbConn; query: SqlQuery; args: varargs[string, `$`]): int64 {.
    ...tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].}
runs the query (typically "UPDATE") and returns the number of affected rows   Source   Edit
proc getAllRows(db: DbConn; query: SqlQuery; args: varargs[string, `$`]): seq[
    Row] {....tags: [ReadDbEffect], raises: [DbError].}
executes the query and returns the whole result dataset.   Source   Edit
proc getRow(db: DbConn; query: SqlQuery; args: varargs[string, `$`]): Row {.
    ...tags: [ReadDbEffect], raises: [DbError].}
Retrieves a single row. If the query doesn't return any rows, this proc will return a Row with empty strings for each column.   Source   Edit
proc getValue(db: DbConn; query: SqlQuery; args: varargs[string, `$`]): string {.
    ...tags: [ReadDbEffect], raises: [DbError].}
executes the query and returns the first column of the first row of the result dataset. Returns "" if the dataset contains no rows or the database value is NULL.   Source   Edit
proc insert(db: DbConn; query: SqlQuery; pkName: string;
            args: varargs[string, `$`]): int64 {....tags: [WriteDbEffect],
    raises: [DbError].}
same as insertId   Source   Edit
proc insertId(db: DbConn; query: SqlQuery; args: varargs[string, `$`]): int64 {.
    ...tags: [WriteDbEffect], raises: [DbError].}
executes the query (typically "INSERT") and returns the generated ID for the row.   Source   Edit
proc len(row: InstantRow): int {.inline, ...raises: [], tags: [].}
Returns number of columns in the row.   Source   Edit
proc open(connection, user, password, database: string): DbConn {.
    ...tags: [DbEffect], raises: [DbError, ValueError].}
opens a database connection. Raises EDb if the connection could not be established.   Source   Edit
proc setEncoding(connection: DbConn; encoding: string): bool {....tags: [DbEffect],
    raises: [].}
sets the encoding of a database connection, returns true for success, false for failure.   Source   Edit
proc tryExec(db: DbConn; query: SqlQuery; args: varargs[string, `$`]): bool {.
    ...tags: [ReadDbEffect, WriteDbEffect], raises: [].}
tries to execute the query and returns true if successful, false otherwise.   Source   Edit
proc tryInsert(db: DbConn; query: SqlQuery; pkName: string;
               args: varargs[string, `$`]): int64 {....tags: [WriteDbEffect],
    raises: [].}
same as tryInsertID   Source   Edit
proc tryInsertId(db: DbConn; query: SqlQuery; args: varargs[string, `$`]): int64 {.
    ...tags: [WriteDbEffect], raises: [].}
executes the query (typically "INSERT") and returns the generated ID for the row or -1 in case of an error.   Source   Edit
proc unsafeColumnAt(row: InstantRow; index: int): cstring {.inline, ...raises: [],
    tags: [].}
Return cstring of given column of the row   Source   Edit

Iterators

iterator fastRows(db: DbConn; query: SqlQuery; args: varargs[string, `$`]): Row {.
    ...tags: [ReadDbEffect], raises: [DbError].}

executes the query and iterates over the result dataset.

This is very fast, but potentially dangerous. Use this iterator only if you require ALL the rows.

Breaking the fastRows() iterator during a loop will cause the next database query to raise an [EDb] exception Commands out of sync.

  Source   Edit
iterator instantRows(db: DbConn; columns: var DbColumns; query: SqlQuery;
                     args: varargs[string, `$`]): InstantRow {.
    ...raises: [DbError], tags: [].}
Same as fastRows but returns a handle that can be used to get column text on demand using []. Returned handle is valid only within the iterator body.   Source   Edit
iterator instantRows(db: DbConn; query: SqlQuery; args: varargs[string, `$`]): InstantRow {.
    ...tags: [ReadDbEffect], raises: [DbError].}
Same as fastRows but returns a handle that can be used to get column text on demand using []. Returned handle is valid only within the iterator body.   Source   Edit
iterator rows(db: DbConn; query: SqlQuery; args: varargs[string, `$`]): Row {.
    ...tags: [ReadDbEffect], raises: [DbError].}
same as fastRows, but slower and safe.   Source   Edit