0Pricing
Electron Desktop App Development · 课时

使用 SQLite 的嵌入式数据库

在 Electron 应用中使用嵌入式 SQLite 数据库存储结构化且可查询的数据,突破键值存储和原始文件的限制。

使用 SQLite 的嵌入式数据库 是 CoddyKit 上的免费 Electron Desktop App Development 课时。 这是第 3 节课,共 3 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Electron Desktop App Development 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Electron Desktop App Development 课程共包含 3 节课。

本课时的部分内容尚未翻译,以英文显示。

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.

常见问题解答

「使用 SQLite 的嵌入式数据库」课时是免费的吗?

是的 — 「使用 SQLite 的嵌入式数据库」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Electron Desktop App Development 课程的其余内容,请升级到 CoddyKit PRO。 Electron Desktop App Development 课程共包含 3 节课。

「使用 SQLite 的嵌入式数据库」这节课中我会学到什么?

在 Electron 应用中使用嵌入式 SQLite 数据库存储结构化且可查询的数据,突破键值存储和原始文件的限制。 你通过在浏览器中直接运行的动手代码来练习 Electron Desktop App Development,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Electron Desktop App Development 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Electron Desktop App Development 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 3 节。

「使用 SQLite 的嵌入式数据库」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Electron Desktop App Development 课中编写并运行代码吗?

能。每节 Electron Desktop App Development 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 本地存储与 IndexedDB
  2. 文件系统访问
  3. 使用 SQLite 的嵌入式数据库
← 返回 Electron Desktop App Development