Usage of Electron and SQLite / Local Storage
Introduction
Electron is a popular framework used for developing desktop applications, and makes it easy to create cross-platform applications using web technologies. SQLite, on the other hand, is a lightweight and embedded database, ideal for meeting data storage needs in such applications. In this article, you will learn how to store data using SQLite in Electron applications.
Creating an Electron Project
First, you need to create your Electron project. Below are the steps needed to create a simple Electron application:
const { app, BrowserWindow } = require('electron');
function createWindow () {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true
}
});
win.loadFile('index.html');
}
app.whenReady().then(createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
Integration with SQLite
To integrate SQLite into your project, you first need to install the 'sqlite3' package. This allows you to configure your SQLite database to interact with your Electron application.
npm install sqlite3
The code below shows the basic setup needed to create the SQLite database and add data:
const sqlite3 = require('sqlite3').verbose();
let db = new sqlite3.Database('./database.db', (err) => {
if (err) {
console.error(err.message);
}
console.log('Connected to the database.');
});
db.serialize(() => {
db.run(`CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)`);
let stmt = db.prepare(`INSERT INTO users (name) VALUES (?)`);
stmt.run('John Doe');
stmt.finalize();
});
db.close();
Conclusion
The combination of Electron and SQLite is an excellent solution for storing data in desktop applications. It allows users to maintain a constant data flow in your application while also improving its performance. In this article, you have learned the basics of the data storage process using SQLite with Electron. By applying this knowledge in your own projects, you can develop robust desktop applications.

Yorum Gönder