当前位置:网站首页>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數據庫會對打包有影響,導致報錯什麼的,結果沒有任何問題。
边栏推荐
- Today, how does sysak implement business jitter monitoring and diagnosis Take you through Anolis OS 25-26
- 开源代码存在安全隐患:一个项目平均有49个漏洞
- R语言使用MatchIt包进行倾向性匹配分析、使用match.data函数构建匹配后的样本集合、使用可视化分析检验倾向性评分匹配后样本中的所有协变量的平衡情况
- R语言使用glm函数构建泊松对数线性回归模型处理三维列联表数据构建饱和模型、使用summary函数获取模型汇总统计信息
- 社恐?自我介绍时大脑一片空白?
- heidisql插入记录,总是出错,要怎么改?
- In 2022, IPv6 deployment and application will be further promoted. How can we comprehensively realize security upgrading and transformation?
- 交换类排序法
- Pule frog 5D flying cinema 5D dynamic cinema experience hall equipment 7d multi person interactive cinema
- 2022陕西省安全员B证操作证考试题库及在线模拟考试
猜你喜欢

HMS core news industry solution: let technology add humanistic temperature

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

Pule frog VR typhoon escape experience VR disaster self rescue simulation system VR popular science knowledge equipment

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

WordPress like hooks and filters in laravel

CVPR 2022 Oral | 以运动为导向的点云单目标跟踪新范式

When the system is reconstructed, I will leave!

rtklib postpos 梳理(以单点定位为例)

高考生父亲深夜自述,最在意的不是孩子成绩,转折点一点都不假

Path Join() and path The difference between resolve()
随机推荐
[JMeter] shell script automatically executes JMeter
6-13 improving load performance - application cache
R语言dplyr包mutate函数将dataframe中的两个数值变量相除创建新的数据列(Create a new variable)
【用户案例-智能制造】数码大方“云”协同,飞跃千山 “保”生产 !
【直播回顾】战码先锋第六期:共建测试子系统,赋能开发者提高代码质量
Introduction to NPM install command
Recommander un logiciel de machine virtuelle pour la construction rapide d'un Cluster d'ordinateurs à puce M1
HMS core news industry solution: let technology add humanistic temperature
从原型链到继承,图解来龙去脉,推荐收藏
Spark streamlined interview
Exchange sort method
Should the theme of the IDE be bright or dark? Here comes the ultimate answer!
Popular understanding of TCP 3-time handshake
Convert the colored liquid image into transparent liquid, and CMU teaches the robot to accurately control how much water is poured into the cup
R语言基于with函数对指定的协变量执行双样本t检验、t.test函数对不同分组对应的协变量的均值进行Welch双样本t检验分析、双独立样本t检验
On the routing tree of gin
ONES 出席首届「精益软件工程大会」,分享效能改进实践
推薦一款M1芯片電腦快速搭建集群的虛擬機軟件
TCP 3次握手的通俗理解
Microsoft edge browser dev 104 is released, and the deep / shallow theme switching is smoother