2.1.0 • Published 7 years ago
jsqlite v2.1.0
jsqlite
JavaScript wrapper for SQLite compiled to JavaScript with Emscripten SDK.
Installation
npm i --save jsqlite
Usage
/* import { Database, Filesystem } from 'jsqlite'; */
const { Database, Filesystem } = require('jsqlite');
/* create a new database */
const db = new Database('example.db');
/* open, execute statements and close */
if (db.open().ok) {
const tableName = 'person';
console.log(db.exec('CREATE TABLE ?? (id INTEGER PRIMARY KEY, name TEXT NOT NULL);', tableName));
console.log(db.exec('INSERT INTO ?? VALUES (null, ?);', tableName, 'John'));
console.log(db.exec('INSERT INTO ?? VALUES (null, ?);', tableName, 'Jane'));
console.log(db.exec('SELECT * FROM ??;', tableName));
console.log(db.exec('SELECT SQLITE_VERSION();'));
db.close();
}
/* delete the database file to avoid useless memory usage */
Filesystem.unlink('example.db');(Re)Compilation
Prerequisites: Emscripten SDK, tar and wget(only if updating) available on PATH.
If you want to use a modified copy of SQLite or to simply update the library cd into ./lib/. Then:
- To compile a modified copy of SQLite place an archive named
sqlite.tar.gzin./lib/containing the modified source code. - In
./lib/execute the following commandsmake && make cleanto rebuild thesqlite.js. If there is nosqlite.tar.gzin./lib/one containing the latest version of SQLite will be pulled from here.
API Reference
Filesystem- Check here for Emscripten filesystem API.
Database { constructor(fileName = '') }- Main class offering SQLite database abstraction and manipulation methods.
fileNameshould be a string representing the database's file name in the virtual filesystem. If it is missing then it is initialized with an empty string and the database becomes temprary(it cannot be dumped, its contents being lost when closed). Check here for other special strings for temporary databases. - Note: Instances of this class are denoted as
dbfrom now on.
- Main class offering SQLite database abstraction and manipulation methods.
db.open() => {code, ok}- Open the connection to the database. Returns an object containing the return code of
sqlite3_openincode, a booleanokto simplify error checking, beingfalsewhen there is an error,trueotherwise. - Note: The database is created in memory and managed internally. To save it to a file use
db.dump()if it isn't temporary.
- Open the connection to the database. Returns an object containing the return code of
db.exec(statement, ...parameters) => {code, ok, statement, result}- Execute the SQL
statementstring, replacing every??and?with every value given inparametersin the same order.??is used for escaping identfiers and?is used for escaping strings. Every parameter will be converted to string before escaping. Returns an object which contains the return code ofsqlite3_execincode, the booleanok, the final(escaped) executed statement instatementand the result of the execution as an array of objects inresult. - Note: To execute a
statementthe database should be opened first.
- Execute the SQL
db.close() => {code, ok}- Close the connection to the database. Returns an object containing the return code of
sqlite3_closeincodeand the booleanok. - Note: The database isn't destroyed when closing it except when the
fileNamerepresents a temporary database.
- Close the connection to the database. Returns an object containing the return code of
db.dump() => Uint8Array- Dump the database. Returns an
Uint8Arraywhich is the binary representation of the database. Throws a linux error code if there is an error. - Note: The returned
Uint8Arrayis an actual SQLite database which can be saved to a file and opened with a SQLite database manager.
- Dump the database. Returns an
db.load(data)- Restore a previous dumped database or an
Uint8Arrayobtained by reading another SQLite database. Throws a linux error code if there is an error. - Note: This action will overwrite the
dbactual content.
- Restore a previous dumped database or an