### Installing sqlite-electron with yarn Source: https://github.com/tmotagam/sqlite-electron/blob/master/README.md This snippet shows how to install the sqlite-electron package using the yarn package manager. It's an alternative to npm for setting up the module. ```bash yarn add sqlite-electron ``` -------------------------------- ### Installing sqlite-electron with npm Source: https://github.com/tmotagam/sqlite-electron/blob/master/README.md This snippet demonstrates how to install the sqlite-electron package using the npm package manager. It's a prerequisite for using the module in an Electron project. ```bash npm install sqlite-electron ``` -------------------------------- ### Basic Electron Main Process Setup with sqlite-electron Source: https://github.com/tmotagam/sqlite-electron/blob/master/README.md This snippet shows the basic structure for integrating `sqlite-electron` within an Electron application's main process. It demonstrates how to require the module and sets up the standard Electron app lifecycle events, emphasizing that `sqlite-electron` should only be used in the main process. ```javascript const { app, BrowserWindow } = require("electron"); const sqlite = require("sqlite-electron"); function createWindow() { // Your Code } app.whenReady().then(() => { // Your Code }); app.on("window-all-closed", () => { // Your Code }); ``` -------------------------------- ### Executing SQL Query Safely with sqlite-electron (Good Practice) Source: https://github.com/tmotagam/sqlite-electron/blob/master/README.md This example illustrates the recommended way to execute an SQL INSERT query using `executeQuery` to prevent SQL injection attacks. Values are passed as an array of parameters, ensuring they are properly escaped by the database driver. ```javascript import { executeQuery } from "sqlite-electron"; executeQuery( "INSERT INTO sqlite_main (NAME,AGE,ADDRESS,SALARY) VALUES (?, ?, ?, ?);", [var_name, var_age, var_address, var_salary] ); // Do this ``` -------------------------------- ### Creating In-Memory SQLite Database in Electron (JavaScript) Source: https://github.com/tmotagam/sqlite-electron/blob/master/README.md This example illustrates how to create a temporary, in-memory SQLite database by passing `:memory:` to the `sqlite.setdbPath` function. In-memory databases are useful for testing or short-lived data, as all data is lost when the application closes. ```javascript const { app, BrowserWindow, ipcMain } = require("electron"); const sqlite = require("sqlite-electron"); function createWindow() { // Your Code } app.whenReady().then(() => { // Your Code }); app.on("window-all-closed", () => { // Your Code }); ipcMain.handle("createInMemoryDatabase", async () => { return await sqlite.setdbPath(":memory:"); }); ``` -------------------------------- ### Executing SQL Query Unsafely with sqlite-electron (Bad Practice) Source: https://github.com/tmotagam/sqlite-electron/blob/master/README.md This example demonstrates an unsafe way to execute an SQL INSERT query using template literals. Directly embedding variables into the query string makes the application vulnerable to SQL injection attacks and should be avoided. ```javascript import { executeQuery } from "sqlite-electron"; executeQuery( `INSERT INTO sqlite_main (NAME,AGE,ADDRESS,SALARY) VALUES (${var_name}, ${var_age}, ${var_address}, ${var_salary});` ); // Never do this ``` -------------------------------- ### Backing Up SQLite Database with sqlite-electron (JavaScript) Source: https://github.com/tmotagam/sqlite-electron/blob/master/README.md This `ipcMain` handler illustrates how to perform a database backup using `sqlite-electron`'s `backup` function. It allows specifying a target database path (relative or absolute), the number of pages to copy per step, a name for the backup, and a sleep duration between steps for controlled backup operations. ```javascript const { app, BrowserWindow, ipcMain } = require("electron"); const sqlite = require("sqlite-electron"); function createWindow() { // Your Code } app.whenReady().then(() => { // Your Code }); app.on("window-all-closed", () => { // Your Code }); icpMain.handle("databasePath", async (event, dbPath) => { return await sqlite.setdbPath(dbPath); }); icpMain.handle("backup", async (event, target, pages, name, sleep) => { return await backup(target, pages, name, sleep); }); ``` -------------------------------- ### Setting Database Path with sqlite-electron in Electron (JavaScript) Source: https://github.com/tmotagam/sqlite-electron/blob/master/README.md This snippet demonstrates how to establish a connection to an existing SQLite database or create a new one using `sqlite.setdbPath` via an `ipcMain` handler. This function must be called before any other database operations to ensure a valid database context is available. ```javascript const { app, BrowserWindow, ipcMain } = require("electron"); const sqlite = require("sqlite-electron"); function createWindow() { // Your Code } app.whenReady().then(() => { // Your Code }); app.on("window-all-closed", () => { // Your Code }); ipcMain.handle("databasePath", async (event, dbPath) => { return await sqlite.setdbPath(dbPath); }); ``` -------------------------------- ### Loading SQLite Extensions with sqlite-electron (JavaScript) Source: https://github.com/tmotagam/sqlite-electron/blob/master/README.md This `ipcMain` handler demonstrates how to load a SQLite extension using `sqlite-electron`'s `load_extension` function. The function requires an absolute path to the extension file. This allows extending SQLite's functionality with custom modules. ```javascript const { app, BrowserWindow, ipcMain } = require("electron"); const sqlite = require("sqlite-electron"); function createWindow() { // Your Code } app.whenReady().then(() => { // Your Code }); app.on("window-all-closed", () => { // Your Code }); icpMain.handle("databasePath", async (event, dbPath) => { return await sqlite.setdbPath(dbPath); }); icpMain.handle("load_extension", async (event, path) => { return await sqlite.load_extension(path); }); ``` -------------------------------- ### Creating Table with SQL Script (SQL) Source: https://github.com/tmotagam/sqlite-electron/blob/master/README.md This SQL snippet defines a `CREATE TABLE` statement for `sqlite_main`. It's intended to be executed as part of a larger script using the `executeScript` function. Note that `executeScript` should not be used for `SELECT` commands as it only returns `true`. ```sql CREATE TABLE IF NOT EXISTS sqlite_main (ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,NAME TEXT NOT NULL,AGE INT NOT NULL,ADDRESS CHAR(50) NOT NULL,SALARY REAL NOT NULL); ``` -------------------------------- ### Executing SQL Scripts with sqlite-electron (JavaScript) Source: https://github.com/tmotagam/sqlite-electron/blob/master/README.md This `ipcMain` handler shows how to use `sqlite-electron`'s `executeScript` function to run SQL commands from a file path or directly from a string. It's suitable for schema creation or other DDL operations, but not for queries expecting results, as it only returns a boolean indicating success. ```javascript const { app, BrowserWindow, ipcMain } = require("electron"); const sqlite = require("sqlite-electron"); function createWindow() { // Your Code } app.whenReady().then(() => { // Your Code }); app.on("window-all-closed", () => { // Your Code }); icpMain.handle("databasePath", async (event, dbPath) => { return await sqlite.setdbPath(dbPath); }); icpMain.handle("executeScript", async (event, scriptpath) => { return await sqlite.executeScript(scriptpath); // or return await sqlite.executeScript( "CREATE TABLE IF NOT EXISTS sqlite_main (ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,NAME TEXT NOT NULL,AGE INT NOT NULL,ADDRESS CHAR(50) NOT NULL,SALARY REAL NOT NULL);" ); }); ``` -------------------------------- ### Fetching Multiple Rows with Limit from SQLite Database in Electron (JavaScript) Source: https://github.com/tmotagam/sqlite-electron/blob/master/README.md This snippet demonstrates how to fetch a specific number of rows using `sqlite.fetchMany`. It takes a SQL query, a `size` parameter to limit the number of returned records, and an optional array of values, returning an array of objects up to the specified limit. ```javascript const { app, BrowserWindow, ipcMain } = require("electron"); const sqlite = require("sqlite-electron"); function createWindow() { // Your Code } app.whenReady().then(() => { // Your Code }); app.on("window-all-closed", () => { // Your Code }); ipcMain.handle("databasePath", async (event, dbPath) => { return await sqlite.setdbPath(dbPath); }); ipcMain.handle("fetchMany", async (event, query, size, values) => { return await sqlite.fetchMany(query, size, values); }); ``` -------------------------------- ### Executing Multiple Insert Queries with sqlite-electron (JavaScript) Source: https://github.com/tmotagam/sqlite-electron/blob/master/README.md This `ipcMain` handler demonstrates how to use `sqlite-electron`'s `executeMany` function to perform batch insertions or updates. It takes a SQL query string with placeholders and an array of arrays, where each inner array represents a set of values for a single row. This method is efficient for inserting multiple records in a single operation. ```javascript const { app, BrowserWindow, ipcMain } = require("electron"); const sqlite = require("sqlite-electron"); function createWindow() { // Your Code } app.whenReady().then(() => { // Your Code }); app.on("window-all-closed", () => { // Your Code }); ipcMain.handle("databasePath", async (event, dbPath) => { return await sqlite.setdbPath(dbPath); }); icpMain.handle("executeMany", async (event, query, values) => { return await sqlite.executeMany(query, values); }); ``` -------------------------------- ### Fetching All Rows from SQLite Database in Electron (JavaScript) Source: https://github.com/tmotagam/sqlite-electron/blob/master/README.md This snippet demonstrates how to retrieve all rows that match a given SQL query using `sqlite.fetchAll`. The function returns an array of objects, where each object represents a row with column names as keys, making it suitable for displaying multiple records. ```javascript const { app, BrowserWindow, ipcMain } = require("electron"); const sqlite = require("sqlite-electron"); function createWindow() { // Your Code } app.whenReady().then(() => { // Your Code }); app.on("window-all-closed", () => { // Your Code }); ipcMain.handle("databasePath", async (event, dbPath) => { return await sqlite.setdbPath(dbPath); }); ipcMain.handle("fetchAll", async (event, query, values) => { return await sqlite.fetchAll(query, values); }); ``` -------------------------------- ### Using SQLite URI Format for Database Path in Electron (JavaScript) Source: https://github.com/tmotagam/sqlite-electron/blob/master/README.md This snippet demonstrates how to specify a database path using the SQLite URI format with `sqlite.setdbPath`. The `isuri=true` parameter indicates that the provided path string should be interpreted as a URI, allowing for advanced connection options like read-write mode. ```javascript const { app, BrowserWindow, ipcMain } = require("electron"); const sqlite = require("sqlite-electron"); function createWindow() { // Your Code } app.whenReady().then(() => { // Your Code }); app.on("window-all-closed", () => { // Your Code }); ipcMain.handle("createDatabaseusingURI", async () => { return await sqlite.setdbPath("file:tutorial.db?mode:rw", isuri=true); }); ``` -------------------------------- ### Executing Single SQL Query with sqlite-electron in Electron (JavaScript) Source: https://github.com/tmotagam/sqlite-electron/blob/master/README.md This snippet shows how to execute any single SQL query, such as INSERT, UPDATE, or DELETE statements, using `sqlite.executeQuery`. It accepts the SQL query string and an optional array of values for parameterized queries, which helps prevent SQL injection vulnerabilities. ```javascript const { app, BrowserWindow, ipcMain } = require("electron"); const sqlite = require("sqlite-electron"); function createWindow() { // Your Code } app.whenReady().then(() => { // Your Code }); app.on("window-all-closed", () => { // Your Code }); ipcMain.handle("databasePath", async (event, dbPath) => { return await sqlite.setdbPath(dbPath); }); ipcMain.handle("executeQuery", async (event, query, values) => { return await sqlite.executeQuery(query, values); }); ``` -------------------------------- ### Fetching Single Row from SQLite Database in Electron (JavaScript) Source: https://github.com/tmotagam/sqlite-electron/blob/master/README.md This snippet illustrates how to fetch only one row from the database using `sqlite.fetchOne`. It is particularly useful when querying for a unique record, such as by an ID, and returns a single object representing the matched row. ```javascript const { app, BrowserWindow, ipcMain } = require("electron"); const sqlite = require("sqlite-electron"); function createWindow() { // Your Code } app.whenReady().then(() => { // Your Code }); app.on("window-all-closed", () => { // Your Code }); ipcMain.handle("databasePath", async (event, dbPath) => { return await sqlite.setdbPath(dbPath); }); ipcMain.handle("fetchOne", async (event, query, values) => { return await sqlite.fetchOne(query, values); }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.