Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
sqlite-electron
Advanced tools
Readme
Sqlite Electron is a module for electron to use sqlite3 database without rebuilding it supports Windows (x64, x32) and Linux (x64). It supports ESM and CJS.
Several bugs fixed and now all data types are supported
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
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.
The example written for this library is not a Boilerplate because of its disregards to the security required for electron so do not use it in your application.
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='') | It opens or creates the database for operation |
executeQuery(Query = '', fetch = '', values = []) | It Executes single query with fetch and values the fetch must be in string eg:- 'all', '1','2'... '' values 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 |
The sqlite-electron should only be required in main process while using 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 3 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);
});
Now you can also create 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:");
});
This is the function for executing any single query eg: 'SELECT * FROM sqlite_main' you can give values through value array and tell the function to fetch data by specifying the fetch parameter eg: "all", 1, 2, 3, .., infinity.
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, fetch, value) => {
return await sqlite.executeQuery(query, fetch, value);
});
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]]) .
Fetch is not available in this function
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 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);"
);
});
See sqlite-electron in action using electron 25.0.1
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
The npm package sqlite-electron receives a total of 29 weekly downloads. As such, sqlite-electron popularity was classified as not popular.
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 1 open source maintainer 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
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.