使用Nim连接到SQLite数据库
问题描述:
我最近开始探索Nim编程语言,我想知道如何连接到SQLite数据库。在阅读了手册的相关部分之后,我的困惑并没有减弱。如果有人愿意提供一个简单的例子,我将不胜感激。使用Nim连接到SQLite数据库
谢谢。
答
尼姆最新的source code提供了一个很好的例子。这里复制例如:
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()
+0
我已经与这个例子中出场为好,我必须从他们的网站下载prebuild sqlite库并将其重命名为'sqlite3_32.dll'并将其放在我的可执行文件附近。 – enthus1ast
一个谷歌搜索'NIM sqlite'给了我这样的:http://nim-lang.org/docs/db_sqlite.html –