当前位置:网站首页>electron添加SQLite數據庫
electron添加SQLite數據庫
2022-06-22 11:16:00 【空城機】
序
在之前,我曾經使用electron開發過一個番茄鐘應用,但是當時的應用數據存儲是在JSON文件當中,通過node的fs文件系統進行讀寫的,但是感覺不用數據庫總有點不太專業。
所以還是打算使用數據庫來作為存儲的地方,不過數據庫的選擇就多了,但是我找了一圈發現適合electron打包進去的數據庫可能還是SQLite吧。
之前也打算使用mongodb,但是必須要下載好,所以直接pass。然後因為SQLite是無需部署無需服務器的,直接node即可創建和操作,所以直接來吧

之前的文章:
開發
因為之前已經寫好了番茄鐘項目,並且將數據的讀寫也單獨分出來了,所以現在新建一個sqliteDB.js作為操作數據庫的方法文件即可。
其實這裏的操作基本參考上面的《node の SQLite》即可。通過sqlite創建數據庫錶的語句先將數據庫tomato.db創建出來,然後創建錶tomatoTable。
創建錶格:(sqlite中Boolean屬性將會被存儲為0/1)
runSQL(` create table tomatoTable ( name text not null, creatTime text not null, duringTime int not null, startFlag boolean not null, bgSrc text not null, taskEndCount int not null, taskId text not null ); `)
然後之前主進程和渲染器進程之間通過ipcMain和ipcRenderer來獲取數據,這一點同樣不變,不過將數據從JSON文件中獲取變成了從sqlite數據庫中獲取。
至於獲取數據庫方法,因為我最初的獲取數據只需要一次性的全部獲取,可以使用db.all,這樣數據在回調方法中顯示,可以通過promise來返回數據,在外面通過await來等待接收。
// 查詢番茄鐘數據
function getTomatoData() {
db = new sqlite3.Database(rootPath);
return new Promise((reslove)=>{
db.all('select * from tomatoTable', (err, data)=>{
reslove(data);
});
})
}

然後在渲染進程中接收時可以直接接收對象了,不需要再通過JSON.parse將字符串轉換對象

因為番茄鐘應用也不用一下子新增或删除多條數據,所以增删改操作也較為簡單:
// 向番茄鐘錶插入新數據
function insertNewInfo(d) {
let insertInfo = db.prepare('insert into tomatoTable (name, creatTime, duringTime, startFlag, bgSrc, taskEndCount, taskId) values (?, ?, ?, ?, ?, ?, ?)')
insertInfo.run(d.name, d.creatTime, d.duringTime, d.startFlag, d.bgSrc, d.taskEndCount, d.taskId );
insertInfo.finalize();
}
// 從番茄鐘錶删除數據
function deleteNewInfo(taskId) {
let del = db.prepare("delete from tomatoTable where taskId = ? ");
del.run(taskId);
del.finalize();
}
// 修改番茄鐘任務完成次數
function updateTaskEndCount(arg) {
let update = db.prepare('update tomatoTable set taskEndCount = ? where taskId = ?');
update.run(arg.taskEndCount, arg.taskId);
update.finalize();
}
在番茄鐘界面新增一條數據,然後可以通過vscode的sqlite插件先查看是否存在:
-- SQLite
select * from tomatoTable

寫到這基本就已經結束了,然後就是將番茄鐘重新打包,方式和《vue番茄鐘&electron 打包》 一樣。
這裏本來以為多出來一個
tomato.db數據庫會對打包有影響,導致報錯什麼的,結果沒有任何問題。
边栏推荐
- 【用户案例-智能制造】数码大方“云”协同,飞跃千山 “保”生产 !
- MAML (Model-Agnostic Meta-Learning) 解读
- Attack and defense drill | threat hunting practice case based on att & CK
- Construction details of Danzhou clean animal laboratory
- Nodejs basic quick review
- Heidisql inserts records. There are always errors. How do you change them?
- Leetcode algorithm refers to offer 24 Reverse linked list
- Redis 切片集群的数据倾斜分析
- [Jenkins] shell script calls Jenkins API interface
- MySQL lock view
猜你喜欢

外贸专题:外贸邮件营销模板

Investment transaction management

机器人强化学习——Sim-to-Real Robot Learning from Pixels with Progressive Nets (2017)

攻防演练 | 基于ATT&CK的威胁狩猎实践案例

今天,SysAK 是如何实现业务抖动监控及诊断?&手把手带你体验Anolis OS|第25-26期

Learn to view object models with VisualStudio developer tools

Pytorch实现波阻抗反演

Yolov3 target detection

In 2022, IPv6 deployment and application will be further promoted. How can we comprehensively realize security upgrading and transformation?

HMS core news industry solution: let technology add humanistic temperature
随机推荐
TCP connection establishment process (in-depth understanding of the source code and three handshakes)
GEE——Global Flood Database v1 (2000-2018)
今天,SysAK 是如何实现业务抖动监控及诊断?&手把手带你体验Anolis OS|第25-26期
Redis 切片集群的数据倾斜分析
[JMeter] shell script automatically executes JMeter
Construction details of Danzhou clean animal laboratory
【直播回顾】战码先锋第六期:共建测试子系统,赋能开发者提高代码质量
PHP website, how to achieve the function of batch printing express orders?
Spark streamlined interview
Native JS dynamically add and delete classes
儋州清洁级动物实验室建设细节说明
HMS core news industry solution: let technology add humanistic temperature
常见面试题目解答之cookie和session
CVPR 2022 Oral | 以运动为导向的点云单目标跟踪新范式
Leetcode algorithm Delete the node of the linked list
Pytoch realizes wave impedance inversion
scrapy. Transfer of meta parameter data of request()
[shell] collection of common instructions
The father of the college entrance examination student told himself at night that what he cared about most was not the child's performance, and the turning point was not false at all
LeetCode Algorithm 21. Merge two ordered linked lists