Sqlite Electron is a module for electron to use sqlite3 database without rebuilding as of now supports Windows(win32) (x64, x32) and Linux (x64).
Use the package manager npm to install Sqlite Electron.
npm install 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 go to https://github.com/tmotagam/sqlite-electron/tree/master/binaries
Functions | Description |
---|---|
dbPath | Variable to set your path for the database and also to connect to the database if it already exists |
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 |
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 exposed variable for setting the path of the new database and also for connecting to the existing database.
Set this variable before using any of the api.
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', (event, dbPath) => {
sqlite.dbPath = dbPath
return true
})
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.
Note: Never give values in the query string use value array for giving the values for the query not taking this precaution will result in sql injection attacks !.
eg: ("INSERT INTO sqlite_main (NAME,AGE,ADDRESS,SALARY) VALUES (?, ?, ?, ?);", ["name", 900, "example street", 123456789000]) // This is best practice
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', (event, dbPath) => {
sqlite.dbPath = dbPath
return true
})
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', (event, dbPath) => {
sqlite.dbPath = dbPath
return true
})
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', (event, dbPath) => {
sqlite.dbPath = dbPath
return true
})
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 19.0.6
Pull requests and issues are welcome. For major changes, please open an issue first to discuss what you would like to change.