A higher level mySQL database wrapper. The same interface is implemented for other databases too.
See also: db_odbc, db_sqlite, db_postgres.
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 (?, ?, ?)"
import db_mysql let db = open("localhost", "user", "password", "dbname") db.close()
db.exec(sql"DROP TABLE IF EXISTS myTable") db.exec(sql("""CREATE TABLE myTable ( id integer, name varchar(50) not null)"""))
db.exec(sql"INSERT INTO myTable (id, name) VALUES (0, ?)", "Dominik")
import 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()
DbConn = distinct PMySQL
Row = seq[string]
InstantRow = object row: cstringArray len: int
proc dbError(db: DbConn) {...}{.noreturn, raises: [DbError], tags: [].}
proc dbQuote(s: string): string {...}{.raises: [], tags: [].}
proc tryExec(db: DbConn; query: SqlQuery; args: varargs[string, `$`]): bool {...}{. tags: [ReadDbEffect, WriteDbEffect], raises: [].}
proc exec(db: DbConn; query: SqlQuery; args: varargs[string, `$`]) {...}{. tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].}
proc `[]`(row: InstantRow; col: int): string {...}{.inline, raises: [], tags: [].}
proc len(row: InstantRow): int {...}{.inline, raises: [], tags: [].}
proc getRow(db: DbConn; query: SqlQuery; args: varargs[string, `$`]): Row {...}{. tags: [ReadDbEffect], raises: [DbError].}
proc getAllRows(db: DbConn; query: SqlQuery; args: varargs[string, `$`]): seq[Row] {...}{. tags: [ReadDbEffect], raises: [DbError].}
proc getValue(db: DbConn; query: SqlQuery; args: varargs[string, `$`]): string {...}{. tags: [ReadDbEffect], raises: [DbError].}
proc tryInsertId(db: DbConn; query: SqlQuery; args: varargs[string, `$`]): int64 {...}{. tags: [WriteDbEffect], raises: [].}
proc insertId(db: DbConn; query: SqlQuery; args: varargs[string, `$`]): int64 {...}{. tags: [WriteDbEffect], raises: [DbError].}
proc execAffectedRows(db: DbConn; query: SqlQuery; args: varargs[string, `$`]): int64 {...}{. tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].}
proc close(db: DbConn) {...}{.tags: [DbEffect], raises: [].}
proc open(connection, user, password, database: string): DbConn {...}{.tags: [DbEffect], raises: [DbError].}
proc setEncoding(connection: DbConn; encoding: string): bool {...}{.tags: [DbEffect], raises: [].}
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
.
iterator instantRows(db: DbConn; query: SqlQuery; args: varargs[string, `$`]): InstantRow {...}{. tags: [ReadDbEffect], raises: [DbError].}
iterator instantRows(db: DbConn; columns: var DbColumns; query: SqlQuery; args: varargs[string, `$`]): InstantRow {...}{.raises: [DbError], tags: [].}
iterator rows(db: DbConn; query: SqlQuery; args: varargs[string, `$`]): Row {...}{. tags: [ReadDbEffect], raises: [DbError].}
© 2006–2018 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/db_mysql.html