代码拉取完成,页面将自动刷新
Features:
importdb
macro to create helper function (see examples):memory:
database since it support WAL mode (Experimental, see tests/test_thread)Basic usage:
import std/tables
import easy_sqlite3
# Bind function argument to sql statment
# The tuple return value indicate the query will got exactly 1 result
proc select_1(arg: int): tuple[value: int] {.importdb: "SELECT $arg".}
var db = initDatabase(":memory:")
# Use as a method (the statment will be cached, thats why `var` is required)
echo db.select_1(1).value
# Got 1
# You can bind create statment as well
proc create_table() {.importdb: """
CREATE TABLE mydata(name TEXT PRIMARY KEY NOT NULL, value INT NOT NULL);
""".}
# Or insert
proc insert_data(name: string, value: int) {.importdb: """
INSERT INTO mydata(name, value) VALUES ($name, $value);
""".}
# And you can create iterator by the same way (the `= discard` is required, since iterator must have body in nim)
iterator iterate_data(): tuple[name: string, value: int] {.importdb: """
SELECT name, value FROM mydata;
""".} = discard
const dataset = {
"A": 0,
"B": 1,
"C": 2,
"D": 3,
}.toTable
db.create_table()
# Use transaction (commit by default)
db.transaction:
for name, value in dataset:
db.insert_data name, value
commit() # optional
# Never goes here
for name, value in db.iterate_data():
assert name in dataset
assert dataset[name] == value
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。