鍍金池/ 教程/ HTML/ SQLite
備忘錄模式
解釋器模式
類似 Python 的 zip 函數(shù)
類變量和實例變量
提示參數(shù)
指數(shù)對數(shù)運算
檢查變量的類型是否為數(shù)組
由數(shù)組創(chuàng)建一個字符串
生成隨機(jī)數(shù)
刪除數(shù)組中的相同元素
大寫單詞首字母
雙向服務(wù)器
類的混合
計算復(fù)活節(jié)的日期
轉(zhuǎn)換弧度和度
找到上一個月(或下一個月)
雙向客戶端
橋接模式
嵌入 JavaScript
AJAX
觀察者模式
克隆對象(深度復(fù)制)
一個隨機(jī)整數(shù)函數(shù)
清理字符串前后的空白符
歸納數(shù)組
平方根倒數(shù)快速算法
適配器模式
打亂數(shù)組中的元素
將數(shù)組連接
使用數(shù)組來交換變量
更快的 Fibonacci 算法
服務(wù)器
服務(wù)端和客戶端的代碼重用
客戶端
查找子字符串
策略模式
CoffeeScrip 的 type 函數(shù)
由數(shù)組創(chuàng)建一個對象詞典
回調(diào)綁定
工廠方法模式
映射數(shù)組
當(dāng)函數(shù)括號不可選
生成可預(yù)測的隨機(jī)數(shù)
不使用 jQuery 的 Ajax 請求
把字符串轉(zhuǎn)換為小寫形式
類方法和實例方法
擴(kuò)展內(nèi)置對象
定義數(shù)組范圍
MongoDB
匹配字符串
創(chuàng)建一個不存在的對象字面值
列表推導(dǎo)
比較范圍
修飾模式
檢測每個元素
拆分字符串
字符串插值
對象數(shù)組
去抖動函數(shù)
使用 Nodeunit 測試
SQLite
單件模式
篩選數(shù)組
替換子字符串
數(shù)組最大值
計算(美國和加拿大的)感恩節(jié)日期
找到一個月中的最后一天
計算兩個日期中間的天數(shù)
基本的 HTTP 服務(wù)器
把字符串轉(zhuǎn)換為大寫形式
使用 HTML 命名實體替換 HTML 標(biāo)簽
For 循環(huán)
模板方法模式
重復(fù)字符串
使用 Jasmine 測試
對象的鏈?zhǔn)秸{(diào)用
數(shù)學(xué)常數(shù)
反轉(zhuǎn)數(shù)組
計算月球的相位
使用 Heregexes
查找子字符串
生成器模式
遞歸函數(shù)
HTTP 客戶端
創(chuàng)建 jQuery 插件
檢測與構(gòu)建丟失的函數(shù)
生成唯一ID
命令模式

SQLite

問題

你需要 Node.js 內(nèi)部與 SQLite 數(shù)據(jù)庫連接的接口。

解決方案

使用 SQLite 模塊。

sqlite = require 'sqlite'

db = new sqlite.Database

# The module uses asynchronous methods,
# so we chain the calls the db.execute
exampleCreate = ->
    db.execute "CREATE TABLE snacks (name TEXT(25), flavor TEXT(25))",
        (exeErr, rows) ->
            throw exeErr if exeErr
            exampleInsert()

exampleInsert = ->
    db.execute "INSERT INTO snacks (name, flavor) VALUES ($name, $flavor)",
        { $name: "Potato Chips", $flavor: "BBQ" },
        (exeErr, rows) ->
            throw exeErr if exeErr
            exampleSelect()

exampleSelect = ->
    db.execute "SELECT name, flavor FROM snacks",
        (exeErr, rows) ->
            throw exeErr if exeErr
            console.log rows[0] # => { name: 'Potato Chips', flavor: 'BBQ' }

# :memory: creates a DB in RAM
# You can supply a filepath (like './example.sqlite') to create/open one on disk
db.open ":memory:", (openErr) ->
    throw openErr if openErr
    exampleCreate()

討論

你也可以提前準(zhǔn)備你的 SQL 查詢語句。

sqlite = require 'sqlite'
async = require 'async' # Not required but added to make the example more concise

db = new sqlite.Database

createSQL = "CREATE TABLE drinks (name TEXT(25), price NUM)"

insertSQL = "INSERT INTO drinks (name, price) VALUES (?, ?)"

selectSQL = "SELECT name, price FROM drinks WHERE price < ?"

create = (onFinish) ->
    db.execute createSQL, (exeErr) ->
        throw exeErr if exeErr
        onFinish()

prepareInsert = (name, price, onFinish) ->
    db.prepare insertSQL, (prepErr, statement) ->
        statement.bindArray [name, price], (bindErr) ->
            statement.fetchAll (fetchErr, rows) -> # Called so that it executes the insert
                onFinish()

prepareSelect = (onFinish) ->
    db.prepare selectSQL, (prepErr, statement) ->
        statement.bindArray [1.00], (bindErr) ->
            statement.fetchAll (fetchErr, rows) ->
                console.log rows[0] # => { name: "Mia's Root Beer", price: 0.75 }
                onFinish()

db.open ":memory:", (openErr) ->
    async.series([
        (onFinish) -> create onFinish,
        (onFinish) -> prepareInsert "LunaSqueeze", 7.95, onFinish,
        (onFinish) -> prepareInsert "Viking Sparkling Grog", 4.00, onFinish,
        (onFinish) -> prepareInsert "Mia's Root Beer", 0.75, onFinish,
        (onFinish) -> prepareSelect onFinish
    ])

SQL 的 SQLite 版本的以及 node-SQLite 模塊文檔提供了更完整的信息。