使用 Nim 连接到 SQLite 数据库

Cai*_*inG 2 sqlite nim-lang

我最近开始探索 Nim 编程语言,我想知道如何连接到 SQLite 数据库。阅读手册的相关部分后,我的困惑并没有减少。如果有人愿意提供一个简单的例子,我将不胜感激。

谢谢。

Rog*_*ger 5

Nim 最新的源代码提供了一个很好的例子。复制这里的例子:

import db_sqlite, math

let theDb = open("mytest.db", nil, nil, nil) # Open mytest.db

theDb.exec(sql"Drop table if exists myTestTbl")

# Create table
theDb.exec(sql("""create table myTestTbl (
    Id    INTEGER PRIMARY KEY,
    Name  VARCHAR(50) NOT NULL,
    i     INT(11),
    f     DECIMAL(18,10))"""))

# Insert
theDb.exec(sql"BEGIN")
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")

# Select
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()
Run Code Online (Sandbox Code Playgroud)