您的位置:首页 > 科技 > 能源 > 在 electron+vite+vue3+express 项目中使用better-sqlite3

在 electron+vite+vue3+express 项目中使用better-sqlite3

2024/10/5 19:21:09 来源:https://blog.csdn.net/qq_45897239/article/details/140440103  浏览:    关键词:在 electron+vite+vue3+express 项目中使用better-sqlite3

文章目录

      • 一、安装 electron-rebuild 和 better-sqlite3
      • 二、使用 electron-rebuild 重建 Node.js 模块
      • 三、better-sqlite3 的基本使用
      • 四、打包
      • 五、参考资料

一、安装 electron-rebuild 和 better-sqlite3

yarn add -D electron-rebuild
yarn add better-sqlite3

Electron 内置的 Node.js 版本和编译到 better-sqlite3 的 Node.js 版本不同将可能导致安装失败,所以此处需要安装 electron-rebuild 重建 Node.js 模块。(注意安装顺序,否则可能出现安装失败的问题)

二、使用 electron-rebuild 重建 Node.js 模块

如果安装完 better-sqlite3 不重建直接运行,则可能出现以下报错:

Error: The module 'xxx'
was compiled against a different Node.js version using
NODE_MODULE_VERSION xxx. This version of Node.js requires
NODE_MODULE_VERSION xxx. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`).

修改 package.json 文件中的 scripts 内容:

  "scripts": {"dev": "vite",..."rebuild": "electron-rebuild -f -w better-sqlite3"},

执行命令:yarn run rebuild 重建 Node.js 版本。

三、better-sqlite3 的基本使用

新建 db/DBManager.ts 文件:

const sqliteDB = require('better-sqlite3');
const path = require('path');const dbPath = process.env.NODE_ENV === "development" ? "./wallpapers.db" : path.join(process.resourcesPath, "./wallpapers.db")// 打开数据库,如果不存在则创建,cacheSize 缓存数据最大值
const db = new sqliteDB(dbPath,{ cacheSize: 15 });
db.pragma('journal_mode = WAL');exports.db = db;

新建 db/ImageManager.ts 文件:

const dbmgr = require("./DBManager.ts");
const db = dbmgr.db;// 读取信息
const readAllImgs = () => {try {const result = db.prepare("SELECT * FROM imgs", { cached: true }).all();return result;} catch (error) {console.error(error);throw error;}
};// 批量插入数据 (values 是一个对象)
const insertWallhaven = (keys, values) => {try {// 使用命名参数 例: $id、$url ... ; 数据 values => [{id: xx, url: xx},{}]// 由于插入的数据 id 可能重复,所以此处使用 OR IGNOREconst insertQuery = db.prepare(`INSERT OR IGNORE INTO wallhaven (${keys.join(",")}) VALUES (${keys.map((item) => "$" + item).join(",")})`);const insertMany = db.transaction((values) => {for (const val of values) {insertQuery.run(val)}});insertMany(values);} catch (error) {console.error(error);throw error;}
};module.exports = {readWallhaven,insertWallhaven,
};

四、打包

修改 electron-builder.json5 文件,添加打包配置:

  "directories": {...},// 添加数据库文件"extraResources": ["./wallpapers.db"],

将数据库文件放在根目录下:

将数据库文件放在根目录下


说明: 后续在使用过程中发现,每次安装新的第三方库时都需要重新执行 yarn run rebuild 命令重建 Node.js 模块,否则可能出现报错。

五、参考资料

  • https://dev.to/arindam1997007/a-step-by-step-guide-to-integrating-better-sqlite3-with-electron-js-app-using-create-react-app-3k16

  • https://www.camelgeek.net/117.html

  • https://github.com/WiseLibs/better-sqlite3

  • https://www.electronjs.org/zh/docs/latest/tutorial/using-native-node-modules

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com