![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
sqlite-electron
Advanced tools
Sqlite Electron is a module for electron to use sqlite3 database without rebuilding it supports Windows (x64, x32) and Linux (x64, arm64). It supports ESM and CJS.
Changes:
Backup the database using the new backup function.
Use the package manager npm to install Sqlite Electron.
npm install sqlite-electron
OR
Use the package manager yarn to install Sqlite Electron.
yarn add sqlite-electron
1. The package installs the prebuilt binaries of the sqlite on your system (if your system is supported) if you want any other platform binaries for a specific version go to https://github.com/tmotagam/sqlite-electron/releases.
2. The examples written for this library disregards the required security for the electron apps so do not use it in your application.
3. Never give values in the query string use values array for giving the values for the query not taking this precaution will result in sql injection attacks !.
Good parctice example
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
Bad parctice example:
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
Api | Description |
---|---|
setdbPath(path='', isuri=false) | It opens or creates the database for operation supports the InMemory databases and also SQLite URI format also the database path can be relative or absolute |
executeQuery(query = '', values = []) | It Executes single query with values they must be array |
executeMany(query = '', values = []) | It executes single query with multiple values |
executeScript(scriptname = '') | It execute the sql script scriptName must be name of the script or the script itself |
fetchAll(query = '', values = []) | It fetches all the values that matches the query. The values can also be given for the query using values array |
fetchOne(query = '', values = []) | It fetches only one value that matches the query. The values can also be given for the query using values array |
fetchMany(query = '', size = 5 values = []) | It fetches as many values as defined in size parameter that matches the query. The values can also be given for the query using values array |
load_extension(path = '') | It loads SQLite extension from the given path for the connected database. |
backup(target='', pages=-1, name='main', sleep=0.250) | It backs up the database to the target database. The pages can be used if the database is very big. The name is used for the database to backup. Sleep is used to pause the operation for the specified seconds between backup of the specified number of pages. |
The sqlite-electron should only be used in main process in electron
example:
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
});
This is a function for opening a existing database or creating a new database for operation.
Call this function before calling the other 7 functions.
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);
});
You can create an In-memory database like this.
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:");
});
You can also use the SQLite URI format like this.
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);
});
This is the function for executing any single query eg: 'INSERT INTO tutorial (x) VALUES (?)' you can give values through the values array.
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);
});
This is the function for fetching all the rows that can be retrived using the given query eg: 'SELECT * from tutorial' you can give values through the values array they will return the data in the Object format like this [{name: 'b', ...}, {name: 'a', ...}, {name: 'c', ...}].
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);
});
This is the function for fetching only one row that can be retrived using the given query eg: 'SELECT * from tutorial WHERE ID=?' you can give values through the values array they will return the data in the Object format like this {name: 'a', ...}.
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);
});
This is the function for fetching as many rows as the size parameter allows that can be retrived using the given query eg: 'SELECT * from tutorial WHERE name=?' you can give values through the values array they will return the data in the Object format like this [{name: 'a', ...}, {name: 'a', ...}, {name: 'a', ...}].
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);
});
This is the function for executing query with multiple values.
eg: ("INSERT INTO sqlite_main (NAME,AGE,ADDRESS,SALARY) VALUES (?, ?, ?, ?)", [["Pa", 32, "California", 20000.00], ["Pau", 32, "California", 20000.00], ["P", 32, "California", 20000.00], ["l", 32, "California", 20000.00]]) .
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("executeMany", async (event, query, values) => {
return await sqlite.executeMany(query, values);
});
This is the function for executing multiple queries using sql scripts this function returns only true so never use any SELECT command in the sql scripts.
You have to give absolute or relative path of the script or give the script`s content directly as well.
eg: script.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);
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("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);"
);
});
This function loads the SQLite extension from the given path for the connected database the path must be absolute.
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("load_extension", async (event, path) => {
return await sqlite.load_extension(path);
});
Backup the database to another database the target database path can be relative or absolute.
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("backup", async (event, target, pages, name, sleep) => {
return await backup(target, pages, name, sleep);
});
See sqlite-electron in action using electron 34.0.0
Pull requests and issues are welcome. For major changes, please open an issue first to discuss what you would like to change.
FAQs
A module for electron to use sqlite3 without rebuilding
We found that sqlite-electron demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.