--- title: sqlite section: sqlite order: 170 --- > **Platform note:** the sqlite library is not available on `linux/mipsle`, > `windows/arm`, or `windows/386` builds (excluded at compile time). `sqlite.open()` returns a connection object. All SQL operations go through that object. Connections are automatically closed when the script exits — including when it throws or calls `exit()` — so a handle can never be leaked; you may also call `db.close()` explicitly to release it early. **Concurrency.** Databases are opened in WAL mode with a 5 second busy timeout, so readers never block the writer and simultaneous requests queue for the write lock instead of failing. If you are writing many rows in a loop, wrap them in [`db.transaction()`](#dbtransactionfn--any) — a batch of N separate `db.exec()` calls costs N lock cycles and N fsyncs, while one transaction costs one of each. ## `sqlite.open(vpath)` → db Opens (or creates) a SQLite database file at the given virtual path. Throws a `SQLiteError` on failure. A user who may only read the drive gets a read only connection, and cannot create a database there. ```javascript requirelib("sqlite"); var db = sqlite.open("user:/.appdata/myapp/data.sqlite"); ``` ## `db.exec(sql, params)` → `{lastInsertId, rowsAffected}` Executes a statement that does not return rows (INSERT, UPDATE, DELETE, CREATE …). `params` is an optional JS array of bound values. ```javascript db.exec("CREATE TABLE IF NOT EXISTS notes (id INTEGER PRIMARY KEY, body TEXT)"); db.exec("INSERT INTO notes (body) VALUES (?)", ["Hello world"]); ``` ## `db.query(sql, params)` → object[] Executes a SELECT and returns all matching rows as an array of plain objects. ```javascript var rows = db.query("SELECT * FROM notes WHERE id > ?", [0]); rows.forEach(function(r) { console.log(r.id, r.body); }); ``` ## `db.queryRow(sql, params)` → object | null Like `db.query()` but returns only the first row, or `null` if no rows matched. ```javascript var row = db.queryRow("SELECT * FROM notes WHERE id = ?", [1]); if (row) sendJSONResp(row); ``` ## `db.tables()` → string[] Returns the names of all user-created tables in the database. ```javascript var tables = db.tables(); sendJSONResp(tables); ``` ## `db.schema(tableName)` → object[] Returns column metadata for the table as an array of `{ cid, name, type, notnull, dflt_value, pk }` objects (from `PRAGMA table_info`). ```javascript var cols = db.schema("notes"); sendJSONResp(cols); ``` ## `db.transaction(fn)` → any Runs `fn` inside a single write transaction, committing when it returns and rolling back if it throws. `fn` receives the connection object, and whatever it returns becomes the return value of `db.transaction()`. Use this whenever you write more than a couple of rows at once: it collapses N lock/fsync cycles into one, and keeps the database available to other requests for far longer. ```javascript db.transaction(function(tx) { for (var i = 0; i < items.length; i++) { tx.exec("INSERT INTO notes (body) VALUES (?)", [items[i]]); } }); ``` The transaction opens with `BEGIN IMMEDIATE`, so the busy timeout applies to acquiring the write lock rather than deadlocking partway through. Transactions cannot be nested — calling `transaction()` inside `fn` throws. ## `db.close()` → bool Closes the database connection and releases the handle. ```javascript db.close(); ``` ## Full sqlite example ```javascript requirelib("sqlite"); var db = sqlite.open("user:/.appdata/myapp/tasks.sqlite"); db.exec("CREATE TABLE IF NOT EXISTS tasks (id INTEGER PRIMARY KEY, title TEXT, done INTEGER DEFAULT 0)"); // Insert var res = db.exec("INSERT INTO tasks (title) VALUES (?)", ["Buy milk"]); console.log("new id:", res.lastInsertId); // Bulk insert — one transaction instead of one per row db.transaction(function(tx) { var titles = ["Walk dog", "Pay rent", "Call mum"]; for (var i = 0; i < titles.length; i++) { tx.exec("INSERT INTO tasks (title) VALUES (?)", [titles[i]]); } }); // Query var pending = db.query("SELECT * FROM tasks WHERE done = 0"); sendJSONResp(pending); db.close(); ```