深圳軟件有限公司企業(yè)網(wǎng)站優(yōu)化關(guān)鍵詞
SQLite
?是一個(gè)進(jìn)程內(nèi)的庫(kù),實(shí)現(xiàn)了自給自足的、無(wú)服務(wù)器的、零配置的、事務(wù)性的?SQL
?數(shù)據(jù)庫(kù)引擎。它是一個(gè)零配置的數(shù)據(jù)庫(kù),這意味著與其他數(shù)據(jù)庫(kù)不一樣,我們不需要在系統(tǒng)中配置。
就像其他數(shù)據(jù)庫(kù),SQLite
?引擎不是一個(gè)獨(dú)立的進(jìn)程,可以按應(yīng)用程序需求進(jìn)行靜態(tài)或動(dòng)態(tài)連接。SQLite
?直接訪問(wèn)其存儲(chǔ)文件。
特性:
- 不需要一個(gè)單獨(dú)的服務(wù)器進(jìn)程或操作的系統(tǒng);
- 一個(gè)完整的?
SQLite
?數(shù)據(jù)庫(kù)是存儲(chǔ)在一個(gè)單一的跨平臺(tái)的磁盤文件中; SQLite
?是自給自足的,這意味著不需要任何外部的依賴;SQLite
?事務(wù)是完全兼容?ACID
?的,允許從多個(gè)進(jìn)程或線程安全訪問(wèn)。
一、SQLite是什么
SQLite是一種嵌入式關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng),是一個(gè)零配置、無(wú)服務(wù)器的、自給自足的、事務(wù)性的SQL數(shù)據(jù)庫(kù)引擎。SQLite是一個(gè)輕量級(jí)的數(shù)據(jù)庫(kù),可以在各種操作系統(tǒng)上使用,并且支持SQL語(yǔ)言標(biāo)準(zhǔn)。
二、SQLite可以做什么
SQLite可以用來(lái)存儲(chǔ)和管理大量的數(shù)據(jù),并且可以通過(guò)SQL語(yǔ)句來(lái)查詢和操作這些數(shù)據(jù)。它可以用于移動(dòng)應(yīng)用程序、桌面應(yīng)用程序、Web應(yīng)用程序、嵌入式系統(tǒng)等等。
三、安裝依賴
?
cnpm i sqlite3 --build-from-source
四、創(chuàng)建數(shù)據(jù)庫(kù)
在electron目錄下新建db文件夾,存放sqlite3db.js文件。
?
sqlite3db.js文件內(nèi)容如下:
//數(shù)據(jù)庫(kù)連接const sqlite3 = require('sqlite3')
const NODE_ENV = process.env.NODE_ENV
const path = require('path')
const { app } = require('electron')
let DB_PATH = path.join(app.getAppPath(), '/config/text.db');console.log('連接數(shù)據(jù)庫(kù)路徑:',app.getAppPath());
console.log('連接數(shù)據(jù)庫(kù)路徑:',DB_PATH);// 判斷是否是正式環(huán)境
if (app.isPackaged) {// 正式環(huán)境DB_PATH = path.join(path.dirname(app.getPath('exe')), '/config/text.db');
}//連接數(shù)據(jù)庫(kù)
function connectDatabase() {return new sqlite3.Database(DB_PATH, (err) => {if (err) {console.error('連接數(shù)據(jù)庫(kù)錯(cuò)誤:' + err.message);}console.log('連接數(shù)據(jù)庫(kù)成功')});
}
const db = connectDatabase();//創(chuàng)建數(shù)據(jù)庫(kù),如果用戶本地沒(méi)有數(shù)據(jù)庫(kù)的話就創(chuàng)建否則跳過(guò)
function createDataTable() {//創(chuàng)建用戶表db.serialize(function () {db.run('create table if not exists user (id INTEGER PRIMARY KEY AUTOINCREMENT, name text, email text, phone text);');});// db.close();
}
exports.connectDatabase = connectDatabase;
exports.createDataTable = createDataTable;
exports.db = db;
/electron/main.js里面引入sqlite3db.js文件。
const { createDataTable } = require("./db/sqlite3db.js")
執(zhí)行
createDataTable();
完整/electron/main.js
const {app,net,ipcMain,BrowserWindow
} = require('electron')
const path = require("path");
const fs = require('fs');
const { checkUpdate } = require("./updater.js")const { createDataTable } = require("./db/sqlite3db.js")const createWindow = () => {const mainWindow = new BrowserWindow({frame: false, //false表示去掉頂部導(dǎo)航去掉關(guān)閉按鈕最大化最小化按鈕width: 1366,height: 768,maxWidth: 1920,minWidth: 1280,minHeight: 600,backgroundColor: '#333',transparent: false, //是否透明webPreferences: {// 允許使用webviewwebviewTag: true,// false關(guān)閉CORS,支持跨域請(qǐng)求webSecurity: false,// 開(kāi)啟渲染進(jìn)程使用node,為了解決require 識(shí)別問(wèn)題nodeIntegration: true,// 是否在獨(dú)立 JavaScript 環(huán)境中運(yùn)行 Electron API和指定的preload 腳本.Electron 12 版本之后它將被默認(rèn)truecontextIsolation: false,// 允許使用remoteenableRemoteModule: true,// 子進(jìn)程路徑preload: path.join(__dirname, "./preload.js"),}})console.log("=====", path.join(__dirname, "./preload.js"));// 判斷是否是正式環(huán)境if (app.isPackaged) {// mainWindow.loadFile(`file://${path.join(__dirname, '../dist/index.html')}`); // 正式環(huán)境下加載html文件mainWindow.loadFile('dist/index.html')// mainWindow.webContents.openDevTools()} else {mainWindow.loadURL('http://127.0.0.1:3000/'); // dev環(huán)境下加載vite服務(wù)頁(yè)面mainWindow.webContents.openDevTools()}createDataTable();
}
app.whenReady().then(() => {createWindow()app.on('activate', () => {if (BrowserWindow.getAllWindows().length === 0) createWindow()})
})
app.on('window-all-closed', () => {if (process.platform !== 'darwin') {app.quit();mainWindow.close();}
})
五、啟動(dòng)腳本,創(chuàng)建數(shù)據(jù)庫(kù)
選擇生成的text.db文件,連接可視化工具
?