sql.js
在網頁上執行 SQLite 的 JavaScript 函式庫。
sql.js 是什麼?
一個可在網頁上執行 SQLite 的 JavaScript 函式庫。
sql.js 概覽
這是什麼
sql.js 是一個 JavaScript 函式庫,透過 Emscripten 將 SQLite 編譯為 WebAssembly,讓您能夠完全在瀏覽器中建立與查詢關聯式資料庫。它使用儲存在記憶體中的虛擬資料庫檔案,不會保留對資料庫所做的變更,但支援匯入現有的 SQLite 檔案,並將資料庫匯出為 JavaScript 型別陣列(Uint8Array)。此外,它還包含了貢獻的數學與字串擴充函式。
主要功能
- 透過 WebAssembly 在瀏覽器中執行 SQLite。
- 在記憶體中建立、查詢與管理關聯式資料庫。
- 匯入現有的 SQLite 資料庫檔案,並將資料庫匯出為 Uint8Array。
- 支援預備陳述式、參數繫結,以及自訂 JavaScript 函式與聚合函式。
- 提供單一檔案設定
sql-wasm-inline.js,其中內嵌了 WebAssembly 二進位檔。 - 可在 Node.js 中使用(Emscripten 6 版本需要 Node.js 18.3 或更新版本)。
使用情境
- 建置需要關聯式資料庫但無需伺服器的用戶端網頁應用程式。
- 在瀏覽器中進行離線資料處理與分析。
- 用於學習 SQL 的教育工具。
- 在瀏覽器環境中原型設計與測試 SQL 查詢。
- 需要匯入/匯出 SQLite 資料庫檔案的應用程式。
快速開始
透過 npm 安裝: bash npm install sql.js 在瀏覽器或 Node.js 中的基本用法: javascript const initSqlJs = require('sql.js'); const SQL = await initSqlJs({ locateFile: file => `https://sql.js.org/dist/${file}` }); const db = new SQL.Database(); db.run('CREATE TABLE hello (a int, b char);'); db.run('INSERT INTO hello VALUES (0, 'hello');'); const res = db.exec('SELECT * FROM hello'); console.log(res); 若想使用無需額外 .wasm 檔案的單一檔案設定,請使用 sql.js/dist/sql-wasm-inline.js。
授權與價格
sql.js 採用 MIT 授權。SQLite 屬於公有領域。此專案為開放原始碼。
內容整理自該專案公開的 README 與儲存庫資訊。