Online visitors

People learning around the world

Online

—

Active in the last 10 minutes

Online visitors by country Circles show approximate country locations. The country list provides the visitor counts.

Circles represent countries, not exact visitor locations.

Country

Online

Loading country activity…

    Node.js Built-in SQLite with node:sqlite

    Node.js Tutorials


    Node.js includes a built-in node:sqlite module for working with SQLite databases without installing a third-party package. SQLite stores relational data in a local file or in memory, making it useful for command-line tools, tests, desktop apps, prototypes, and lightweight services.

    Use Node.js 22.13 or later for these examples without the experimental SQLite flag. Save the code in sqlite-demo.cjs so require() works even in an ES-module project.

    Import DatabaseSync

    Example:

    const { DatabaseSync } = require('node:sqlite');

    DatabaseSync provides a synchronous API. That keeps examples simple, but long-running database work can block the event loop, so use it thoughtfully in servers.

    Create a Database

    Example:

    const { DatabaseSync } = require('node:sqlite');
    
    const db = new DatabaseSync(':memory:');
    
    db.exec(`
      CREATE TABLE users (
        id INTEGER PRIMARY KEY,
        name TEXT NOT NULL
      )
    `);

    :memory: creates a temporary database. The remaining snippets continue this same script; keep a single DatabaseSync import at the top. Use a filename such as app.db for persistent storage.

    Insert Data Safely

    Prepared statements keep values separate from SQL text and help prevent SQL injection.

    Example:

    const insert = db.prepare(
      'INSERT INTO users (name) VALUES (?)'
    );
    
    insert.run('Asha');
    insert.run('Noah');

    Read Rows

    Example:

    const rows = db
      .prepare('SELECT id, name FROM users ORDER BY id')
      .all();
    
    for (const row of rows) {
      console.log(row.id, row.name);
    }

    Output:

    1 Asha
    2 Noah

    Read One Row

    Example:

    const user = db
      .prepare('SELECT id, name FROM users WHERE id = ?')
      .get(1);
    
    console.log(user ? user.name : "Not found");

    Use get() when you expect one row and all() when you need all matching rows.

    Use Transactions

    Example:

    db.exec('BEGIN');
    
    try {
      insert.run('Mia');
      insert.run('Ravi');
      db.exec('COMMIT');
    } catch (error) {
      db.exec('ROLLBACK');
      throw error;
    }

    A transaction keeps related changes together so they either all succeed or all roll back.

    Close the Database

    Example:

    db.close();
    Method Purpose
    exec() Run fixed SQL such as schema creation
    prepare() Create a prepared statement
    run() Execute an insert, update, or delete
    get() Read one row
    all() Read multiple rows

    Use bound parameters for external values instead of concatenating user input into SQL strings.

    Run the assembled script with node sqlite-demo.cjs. The Node.js 22 SQLite module may print an experimental warning separately from the query output.

    Conclusion

    The built-in node:sqlite module gives Node.js a direct way to use SQLite for local relational storage. Start with prepared statements and in-memory databases, then move to file-backed databases and transactions when the application needs persistent data.



    Found This Page Useful? Share It!
    Get the Latest Tutorials and Updates
    Join us on Telegram

    Notifications

    Updates are enabled for this browser.

    Receive new tutorials, articles and learning resources. You can turn updates off anytime.

    Test browser code

    Use this code in admin to send a test notification to this browser.