2015-10-18 324 views
2

我最近開始探索Nim編程語言,我想知道如何連接到SQLite數據庫。在閱讀了手冊的相關部分之後,我的困惑並沒有減弱。如果有人願意提供一個簡單的例子,我將不勝感激。使用Nim連接到SQLite數據庫

謝謝。

+2

一個谷歌搜索'NIM sqlite'給了我這樣的:http://nim-lang.org/docs/db_sqlite.html –

回答

4

尼姆最新的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