0Pricing
Electron Desktop App Development · Lesson

Embedded Databases with SQLite

Store structured, queryable data in your Electron app using an embedded SQLite database, going beyond key-value storage and raw files.

Embedded Databases with SQLite is a free Electron Desktop App Development lesson on CoddyKit — lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Electron Desktop App Development learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

When Files Are Not Enough

Key-value storage and flat files work for small data. For relational, queryable data, an embedded database like SQLite is the right tool.

Why SQLite for Desktop

SQLite is serverless and stores everything in a single file. It ships inside your app, no installation needed by the user.

Choosing a Driver

Popular Node drivers include better-sqlite3 (synchronous, fast) and sqlite3 (async). Run database code in the main process.

const Database = require('better-sqlite3');
const db = new Database('app.db');

Creating a Table

Define your schema once with CREATE TABLE IF NOT EXISTS so it is safe to run on every launch.

db.exec('CREATE TABLE IF NOT EXISTS notes (id INTEGER PRIMARY KEY, body TEXT)');

Inserting Rows

Use prepared statements with placeholders to insert data safely.

const insert = db.prepare('INSERT INTO notes (body) VALUES (?)');
insert.run('Buy groceries');

Querying Data

Read rows back with all() for many or get() for one.

function buildQuery(table) {
  return 'SELECT * FROM ' + table;
}
console.log(buildQuery('notes'));

Parameterize Everything

Never concatenate user input into SQL. Placeholders prevent SQL injection and quoting bugs.

function isSafe(query) {
  return query.includes('?');
}
console.log(isSafe('SELECT * FROM notes WHERE id = ?'));

Exposing Data to the Renderer

The renderer should never touch the DB directly. Expose query functions through a preload bridge over IPC.

ipcMain.handle('notes:list', () => {
  return db.prepare('SELECT * FROM notes').all();
});

Migrations

As your schema evolves, track a user_version and apply migrations step by step on startup.

Where to Store the File

Place the database in the user-data directory via app.getPath('userData') so it survives updates and is writable.

const path = require('path');
function dbPath(userData) {
  return path.join(userData, 'app.db');
}
console.log(dbPath('/home/user/.config/myapp'));

Transactions for Safety

Wrap multiple writes in a transaction so they all succeed or all roll back, keeping data consistent.

Quick Check

Test your SQLite knowledge.

Recap

You learned to add structured persistence with SQLite: choosing a driver, creating tables, using prepared statements, exposing data over IPC, storing the file in userData, and using transactions and migrations.

Frequently asked questions

Is the “Embedded Databases with SQLite” lesson free?

Yes — the full text of “Embedded Databases with SQLite” is free to read here on the web, and the Electron Desktop App Development course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Electron Desktop App Development course, upgrade to CoddyKit PRO.

What will I learn in “Embedded Databases with SQLite”?

Store structured, queryable data in your Electron app using an embedded SQLite database, going beyond key-value storage and raw files. You practise Electron Desktop App Development with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Electron Desktop App Development?

No prior experience is required. Electron Desktop App Development on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Embedded Databases with SQLite” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Electron Desktop App Development lesson?

Yes. Every Electron Desktop App Development lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Local Storage & IndexedDB
  2. File System Access
  3. Embedded Databases with SQLite
← Back to Electron Desktop App Development