sqlite

A fork of db_sqlite, Nim's standard library higher level SQLite database wrapper.

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

The following examples are same as for db_sqlite.

Opening a connection to a database

import db_sqlite
let db = open("mytest.db", "", "", "")  # user, password, database name can be nil
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, ?)",
        "Jack")

Larger example

import db_sqlite, math

let db = open("mytest.db", "", "", "")

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

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

for x in db.rows(sql"select * from myTestTbl"):
  echo x

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

db.close()

See also

Types

DbValueKind = enum
  dvkInt,                     ## SQLITE_INTEGER, 64-bit signed integer
  dvkFloat,                   ## SQLITE_FLOAT, 64-bit IEEE floating point number
  dvkString,                  ## SQLITE_TEXT, string
  dvkBlob,                    ## SQLITE_BLOB, BLOB
  dvkNull                     ## SQLITE_NULL, NULL
Kind of value, corresponds to one of SQLite Fundamental Datatypes.
DbValueTypes = int64 | float64 | string | DbBlob | DbNull
Possible value types
DbBlob = distinct string
SQLite BLOB value.
DbNull = object
SQLite NULL value.
DbValue = object
  case kind*: DbValueKind
  of dvkInt:
      i*: int64

  of dvkFloat:
      f*: float64

  of dvkString:
      s*: string

  of dvkBlob:
      b*: DbBlob

  of dvkNull:
      nil

  
SQLite value.
DbConn = PSqlite3
encapsulates a database connection
Row = seq[DbValue]
a row of a dataset
InstantRow = PStmt
a handle that can be used to get a row's column text on demand

Procs

proc `==`(a: DbBlob; b: DbBlob): bool {...}{.raises: [], tags: [].}
Compare two blobs.
proc `==`(a: DbValue; b: DbValue): bool {...}{.raises: [], tags: [].}
Compare two DB values.
proc dbError(db: DbConn) {...}{.noreturn, raises: [DbError], tags: [].}
Raises a DbError exception.
proc dbQuote(s: string): string {...}{.raises: [], tags: [].}
DB quotes the string. Escaping values to generate SQL queries is not recommended, bind values using the ? (question mark) instead.
proc dbQuote(s: DbBlob): string {...}{.raises: [], tags: [].}
DB quotes the blob.
proc `$`(v: DbValue): string {...}{.raises: [], tags: [].}
proc dbValue(v: DbValue): DbValue {...}{.raises: [], tags: [].}
Return v as is.
proc dbValue(v: int | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32): DbValue
Wrap integer value.
proc dbValue(v: float64): DbValue {...}{.raises: [], tags: [].}
Wrap float value.
proc dbValue(v: string): DbValue {...}{.raises: [], tags: [].}
Wrap string value.
proc dbValue(v: DbBlob): DbValue {...}{.raises: [], tags: [].}
Wrap BLOB value.
proc dbValue(v: DbNull | type(nil)): DbValue
Wrap NULL value. Caveat: dbValue(nil) doesn't compile on Nim 0.19.x, see https://github.com/nim-lang/Nim/pull/9231.
proc dbValue[T](v: Option[T]): DbValue
Wrap value of type T or NULL.
proc tryExec(db: DbConn; query: SqlQuery; args: varargs[DbValue, dbValue]): bool {...}{.
    tags: [ReadDbEffect, WriteDbEffect], raises: [].}
Tries to execute the query and returns true if successful, false otherwise.
proc exec(db: DbConn; query: SqlQuery; args: varargs[DbValue, dbValue]) {...}{.
    tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].}
Executes the query and raises DbError if not successful.
proc `[]`(row: InstantRow; col: int32; T: typedesc = string): T:type {...}{.inline.}
Return value for given column of the row. T has to be one of DbValueTypes or DbValue.
proc len(row: InstantRow): int32 {...}{.inline, raises: [], tags: [].}
Return number of columns in the row.
proc getRow(db: DbConn; query: SqlQuery; args: varargs[DbValue, dbValue]): Option[Row] {...}{.
    tags: [ReadDbEffect], raises: [DbError].}
Retrieves a single row.
proc getAllRows(db: DbConn; query: SqlQuery; args: varargs[DbValue, dbValue]): seq[Row] {...}{.
    tags: [ReadDbEffect], raises: [DbError].}
Executes the query and returns the whole result dataset.
proc getValue[T: DbValueTypes | DbValue](db: DbConn; query: SqlQuery;
                                     args: varargs[DbValue, dbValue]): Option[T] {...}{.
    tags: [ReadDbEffect].}
Executes the query and returns the first column of the first row of the result dataset.
proc getValue(db: DbConn; T: typedesc; query: SqlQuery; args: varargs[DbValue, dbValue]): Option[
    T] {...}{.tags: [ReadDbEffect].}
proc tryInsertID(db: DbConn; query: SqlQuery; args: varargs[DbValue, dbValue]): int64 {...}{.
    tags: [WriteDbEffect], raises: [].}
Executes the query (typically "INSERT") and returns the generated ID for the row or -1 in case of an error.
proc insertID(db: DbConn; query: SqlQuery; args: varargs[DbValue, dbValue]): int64 {...}{.
    tags: [WriteDbEffect], raises: [DbError].}
Executes the query (typically "INSERT") and returns the generated ID for the row. For Postgre this adds RETURNING id to the query, so it only works if your primary key is named id.
proc execAffectedRows(db: DbConn; query: SqlQuery; args: varargs[DbValue, dbValue]): int64 {...}{.
    tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].}
Executes the query (typically "UPDATE") and returns the number of affected rows.
proc close(db: DbConn) {...}{.tags: [DbEffect], raises: [DbError].}
Closes the database connection.
proc open(connection, user, password, database: string): DbConn {...}{.tags: [DbEffect],
    raises: [DbError].}
opens a database connection. Raises EDb if the connection could not be established. Only the connection parameter is used for sqlite.
proc setEncoding(connection: DbConn; encoding: string): bool {...}{.tags: [DbEffect],
    raises: [DbError].}

Sets the encoding of a database connection, returns true for success, false for failure.

Note that the encoding cannot be changed once it's been set. According to SQLite3 documentation, any attempt to change the encoding after the database is created will be silently ignored.

Iterators

iterator rows(db: DbConn; query: SqlQuery; args: varargs[DbValue, dbValue]): Row {...}{.
    tags: [ReadDbEffect], raises: [DbError, DbError].}
Executes the query and iterates over the result dataset.
iterator instantRows(db: DbConn; query: SqlQuery; args: varargs[DbValue, dbValue]): InstantRow {...}{.
    tags: [ReadDbEffect], raises: [DbError, DbError].}
Same as rows but returns a handle that can be used to get column values on demand using []. Returned handle is valid only within the iterator body.
iterator instantRows(db: DbConn; columns: var DbColumns; query: SqlQuery;
                    args: varargs[DbValue, dbValue]): InstantRow {...}{.
    tags: [ReadDbEffect], raises: [DbError, DbError].}
Same as rows but returns a handle that can be used to get column values on demand using []. Returned handle is valid only within the iterator body.
iterator fastRows(db: DbConn; query: SqlQuery; args: varargs[DbValue, dbValue]): Row {...}{.
    tags: [ReadDbEffect], deprecated, raises: [DbError].}
Deprecated
Deprecated: use rows instead.

Templates

template `?`(v: typed): DbValue
Shortcut for dbValue.