1 Star 0 Fork 0

Sopadilla / easy_sqlite3

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

Yet another SQLite wrapper for Nim

Features:

  1. Design for ARC/ORC, you don’t need to close the connection manually
  2. Use importdb macro to create helper function (see examples)
  3. Including a memfs implemention, may better than :memory: database since it support WAL mode (Experimental, see tests/test_thread)

Example

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

空文件

简介

暂无描述 展开 收起
C 等 2 种语言
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
C
1
https://gitee.com/Sopadilla/easy_sqlite3.git
git@gitee.com:Sopadilla/easy_sqlite3.git
Sopadilla
easy_sqlite3
easy_sqlite3
develop

搜索帮助