ฐานข้อมูล SQLite แบบฝังตัว
จัดเก็บข้อมูลที่มีโครงสร้างและสืบค้นได้ในแอป Electron ด้วยฐานข้อมูล SQLite แบบฝังตัว ซึ่งก้าวข้ามการจัดเก็บแบบคีย์-ค่าและไฟล์ดิบ
ฐานข้อมูล SQLite แบบฝังตัว เป็นบทเรียน Electron Desktop App Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 3 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Electron Desktop App Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Electron Desktop App Development มีบทเรียนทั้งหมด 3 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ฐานข้อมูล SQLite แบบฝังตัว”
จัดเก็บข้อมูลที่มีโครงสร้างและสืบค้นได้ในแอป Electron ด้วยฐานข้อมูล SQLite แบบฝังตัว ซึ่งก้าวข้ามการจัดเก็บแบบคีย์-ค่าและไฟล์ดิบ คุณปฏิบัติ Electron Desktop App Development ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Electron Desktop App Development หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Electron Desktop App Development บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 3 บทเรียน
บทเรียน “ฐานข้อมูล SQLite แบบฝังตัว” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Electron Desktop App Development นี้ได้ไหม
ได้ บทเรียน Electron Desktop App Development ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- พื้นที่จัดเก็บในเครื่องและ IndexedDB
- การเข้าถึงระบบไฟล์
- ฐานข้อมูล SQLite แบบฝังตัว