0Pricing
Electron Desktop App Development · Lezione

Database incorporati con SQLite

Memorizzate dati strutturati e interrogabili nella vostra app Electron usando un database SQLite incorporato, andando oltre l’archiviazione chiave-valore e i file grezzi.

Database incorporati con SQLite è una lezione Electron Desktop App Development gratuita su CoddyKit. Questa è la lezione 3 di 3. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Electron Desktop App Development, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Electron Desktop App Development include 3 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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.

Domande Frequenti

La lezione «Database incorporati con SQLite» è gratuita?

Sì — il testo completo di «Database incorporati con SQLite» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Electron Desktop App Development, passa a CoddyKit PRO. Il corso Electron Desktop App Development include 3 lezioni in totale.

Cosa imparerò in «Database incorporati con SQLite»?

Memorizzate dati strutturati e interrogabili nella vostra app Electron usando un database SQLite incorporato, andando oltre l’archiviazione chiave-valore e i file grezzi. Eserciti Electron Desktop App Development con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Electron Desktop App Development?

Non è richiesta alcuna esperienza precedente. Electron Desktop App Development su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 3 di 3.

Quanto tempo richiede la lezione «Database incorporati con SQLite»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Electron Desktop App Development?

Sì. Ogni lezione Electron Desktop App Development include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Archiviazione locale e IndexedDB
  2. Accesso al file system
  3. Database incorporati con SQLite
← Torna a Electron Desktop App Development