Electron中如何使用SQLite数据库
<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li>介绍</li><li>准备工作</li><li>安装SQLite</li><li>连接SQLite数据库</li><li>创建表格</li><li>插入数据</li><li>查询数据</li><li>更新数据</li><li>删除数据</li><li>使用Sequelize</li></ul></div><p>Electron 应用中常使用的数据库是 SQLite、IndexedDB,IndexedDB 是在前端网页中去操作。有的文章里说 IndexedDB 的性能会比 SQLite 更好,大家看实际场景去选择使用。大多数桌面应用或者 App 需要使用数据库的时候一般都是用 SQLite。</p><p>npm 上有两个最常用的 sqlite3 库,一是 better-sqlite3 ,一是 node-sqlite ,两种各有特点。前者是同步的 api ,执行速度快,后者是异步 api ,执行速度相对慢一点。值得注意的是,后者的编译支持 arm 机器,而且由于出的比较早,和其他库配合使用很方便。</p><p class="maodian"></p><h2>介绍</h2><p>Electron是一个流行的跨平台框架,用于构建桌面应用程序。它结合了Chromium和Node.js,使开发者能够使用Web技术(HTML、CSS和JavaScript)构建强大的本地应用程序。SQLite是一种轻量级的关系型数据库引擎,经常用于嵌入式系统和移动应用程序中。</p><p>本文将介绍如何在Electron应用程序中使用SQLite数据库。我们将探讨如何安装SQLite,执行基本的CRUD操作,以及使用一些流行的Node.js库来简化SQLite的使用。</p><p class="maodian"></p><h2>准备工作</h2><p>在开始之前,我们需要先安装一些必要的依赖库:</p><ul class=" list-paddingleft-2"><li>Git</li><li>Node.js</li><li>Electron</li></ul><p>安装完成后,我们可以继续进行后续的步骤。</p><p class="maodian"></p><h2>安装SQLite</h2><p>在Electron中使用SQLite之前,我们需要先安装SQLite。SQLite可以通过访问官方网站下载和安装。</p><p>安装完成后,我们需要在Electron项目的根目录中创建一个新的文件夹,用于存放SQLite数据库文件。</p><p class="maodian"></p><h2>连接SQLite数据库</h2><p>在Electron应用程序中,我们可以使用<code>sqlite3</code>模块来连接和操作SQLite数据库。首先,我们需要使用npm安装<code>sqlite3</code>模块:</p><div style="box-sizing: border-box; position: relative; color: rgb(33, 33, 33);"><pre style="box-sizing: border-box; overflow: auto; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: 20px;">npm install sqlite3</pre><div style="box-sizing: border-box; position: absolute; top: 0.3em; right: 0.2em; transition: opacity 0.3s ease-in-out 0s; opacity: 0;"><div><span style="box-sizing: border-box; color: rgb(187, 187, 187); font-size: 0.8em; padding: 0px 0.5em; background: rgba(224, 224, 224, 0.2); box-shadow: rgba(0, 0, 0, 0.2) 0px 2px 0px 0px; border-radius: 0.5em;">Bash</span></div><div><button style="margin: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 0.8em; font-family: inherit; color: rgb(187, 187, 187); overflow: visible; appearance: button; cursor: pointer; background: rgba(224, 224, 224, 0.2); border-width: 0px; border-style: initial; border-color: initial; padding: 0px 0.5em; user-select: none; box-shadow: rgba(0, 0, 0, 0.2) 0px 2px 0px 0px; border-radius: 0.5em;">Copy</button></div></div></div><p>然后,在我们的Electron主进程文件(通常是<code>main.js</code>或<code>index.js</code>)中,我们可以按照以下步骤连接SQLite数据库:</p><div style="box-sizing: border-box; position: relative; color: rgb(33, 33, 33);"><pre style="box-sizing: border-box; overflow: auto; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: 20px;">const sqlite3 = require('sqlite3').verbose();const path = require('path');const dbPath = path.join(__dirname, './database/mydatabase.db');const db = new sqlite3.Database(dbPath);</pre><div style="box-sizing: border-box; position: absolute; top: 0.3em; right: 0.2em; transition: opacity 0.3s ease-in-out 0s; opacity: 0;"><div><span style="box-sizing: border-box; color: rgb(187, 187, 187); font-size: 0.8em; padding: 0px 0.5em; background: rgba(224, 224, 224, 0.2); box-shadow: rgba(0, 0, 0, 0.2) 0px 2px 0px 0px; border-radius: 0.5em;">JavaScript</span></div><div><button style="margin: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 0.8em; font-family: inherit; color: rgb(187, 187, 187); overflow: visible; appearance: button; cursor: pointer; background: rgba(224, 224, 224, 0.2); border-width: 0px; border-style: initial; border-color: initial; padding: 0px 0.5em; user-select: none; box-shadow: rgba(0, 0, 0, 0.2) 0px 2px 0px 0px; border-radius: 0.5em;">Copy</button></div></div></div><p>在这个示例中,我们使用了<code>sqlite3</code>模块来进行SQLite数据库的连接,并通过<code>path</code>模块构建了数据库的路径。然后,我们创建了一个<code>Database</code>对象,该对象表示数据库的连接。</p><p class="maodian"></p><h2>创建表格</h2><p>接下来,我们可以使用<code>db.run()</code>方法创建一个新的表格。下面是一个简单的示例:</p><div style="box-sizing: border-box; position: relative; color: rgb(33, 33, 33);"><pre style="box-sizing: border-box; overflow: auto; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: 20px;">db.run('CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER)');</pre><div style="box-sizing: border-box; position: absolute; top: 0.3em; right: 0.2em; transition: opacity 0.3s ease-in-out 0s; opacity: 0;"><div><span style="box-sizing: border-box; color: rgb(187, 187, 187); font-size: 0.8em; padding: 0px 0.5em; background: rgba(224, 224, 224, 0.2); box-shadow: rgba(0, 0, 0, 0.2) 0px 2px 0px 0px; border-radius: 0.5em;">JavaScript</span></div><div><button style="margin: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 0.8em; font-family: inherit; color: rgb(187, 187, 187); overflow: visible; appearance: button; cursor: pointer; background: rgba(224, 224, 224, 0.2); border-width: 0px; border-style: initial; border-color: initial; padding: 0px 0.5em; user-select: none; box-shadow: rgba(0, 0, 0, 0.2) 0px 2px 0px 0px; border-radius: 0.5em;">Copy</button></div></div></div><p>在这个示例中,我们使用<code>db.run()</code>方法创建了一个名为<code>users</code>的表格,并定义了三个列:<code>id</code>、<code>name</code>和<code>age</code>。</p><p class="maodian"></p><h2>插入数据</h2><p>要向表格中插入数据,我们可以使用<code>db.run()</code>方法和SQL INSERT语句。下面是一个示例:</p><div style="box-sizing: border-box; position: relative; color: rgb(33, 33, 33);"><pre style="box-sizing: border-box; overflow: auto; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: 20px;">db.run('INSERT INTO users (name, age) VALUES (?, ?)', ['John', 25]);</pre><div style="box-sizing: border-box; position: absolute; top: 0.3em; right: 0.2em; transition: opacity 0.3s ease-in-out 0s; opacity: 0;"><div><span style="box-sizing: border-box; color: rgb(187, 187, 187); font-size: 0.8em; padding: 0px 0.5em; background: rgba(224, 224, 224, 0.2); box-shadow: rgba(0, 0, 0, 0.2) 0px 2px 0px 0px; border-radius: 0.5em;">JavaScript</span></div><div><button style="margin: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 0.8em; font-family: inherit; color: rgb(187, 187, 187); overflow: visible; appearance: button; cursor: pointer; background: rgba(224, 224, 224, 0.2); border-width: 0px; border-style: initial; border-color: initial; padding: 0px 0.5em; user-select: none; box-shadow: rgba(0, 0, 0, 0.2) 0px 2px 0px 0px; border-radius: 0.5em;">Copy</button></div></div></div><p>在这个示例中,我们使用了SQL参数化查询来处理用户输入,以避免SQL注入攻击。</p><p class="maodian"></p><h2>查询数据</h2><p>要从表格中查询数据,我们可以使用<code>db.all()</code>方法和SQL SELECT语句。下面是一个示例:</p><div style="box-sizing: border-box; position: relative; color: rgb(33, 33, 33);"><pre style="box-sizing: border-box; overflow: auto; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: 20px;">db.all('SELECT * FROM users', (err, rows) => { if (err) {
console.error(err);
return;
}
rows.forEach((row) => {
console.log(row.id, row.name, row.age);
});});</pre><div style="box-sizing: border-box; position: absolute; top: 0.3em; right: 0.2em; transition: opacity 0.3s ease-in-out 0s; opacity: 0;"><div><span style="box-sizing: border-box; color: rgb(187, 187, 187); font-size: 0.8em; padding: 0px 0.5em; background: rgba(224, 224, 224, 0.2); box-shadow: rgba(0, 0, 0, 0.2) 0px 2px 0px 0px; border-radius: 0.5em;">JavaScript</span></div><div><button style="margin: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 0.8em; font-family: inherit; color: rgb(187, 187, 187); overflow: visible; appearance: button; cursor: pointer; background: rgba(224, 224, 224, 0.2); border-width: 0px; border-style: initial; border-color: initial; padding: 0px 0.5em; user-select: none; box-shadow: rgba(0, 0, 0, 0.2) 0px 2px 0px 0px; border-radius: 0.5em;">Copy</button></div></div></div><p>在这个示例中,我们使用了<code>db.all()</code>方法从<code>users</code>表格中选择所有的行,并使用回调函数处理查询结果。</p><p class="maodian"></p><h2>更新数据</h2><p>要更新表格中的数据,我们可以使用<code>db.run()</code>方法和SQL UPDATE语句。下面是一个示例:</p><div style="box-sizing: border-box; position: relative; color: rgb(33, 33, 33);"><pre style="box-sizing: border-box; overflow: auto; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: 20px;">db.run('UPDATE users SET age = ? WHERE name = ?', );</pre><div style="box-sizing: border-box; position: absolute; top: 0.3em; right: 0.2em; transition: opacity 0.3s ease-in-out 0s; opacity: 0;"><div><span style="box-sizing: border-box; color: rgb(187, 187, 187); font-size: 0.8em; padding: 0px 0.5em; background: rgba(224, 224, 224, 0.2); box-shadow: rgba(0, 0, 0, 0.2) 0px 2px 0px 0px; border-radius: 0.5em;">JavaScript</span></div><div><button style="margin: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 0.8em; font-family: inherit; color: rgb(187, 187, 187); overflow: visible; appearance: button; cursor: pointer; background: rgba(224, 224, 224, 0.2); border-width: 0px; border-style: initial; border-color: initial; padding: 0px 0.5em; user-select: none; box-shadow: rgba(0, 0, 0, 0.2) 0px 2px 0px 0px; border-radius: 0.5em;">Copy</button></div></div></div><p>在这个示例中,我们使用了SQL参数化查询来更新名为<code>John</code>的用户的年龄为30。</p><p class="maodian"></p><h2>删除数据</h2><p>要删除表格中的数据,我们可以使用<code>db.run()</code>方法和SQL DELETE语句。下面是一个示例:</p><div style="box-sizing: border-box; position: relative; color: rgb(33, 33, 33);"><pre style="box-sizing: border-box; overflow: auto; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: 20px;">db.run('DELETE FROM users WHERE age > ?', );</pre><div style="box-sizing: border-box; position: absolute; top: 0.3em; right: 0.2em; transition: opacity 0.3s ease-in-out 0s; opacity: 0;"><div><span style="box-sizing: border-box; color: rgb(187, 187, 187); font-size: 0.8em; padding: 0px 0.5em; background: rgba(224, 224, 224, 0.2); box-shadow: rgba(0, 0, 0, 0.2) 0px 2px 0px 0px; border-radius: 0.5em;">JavaScript</span></div><div><button style="margin: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 0.8em; font-family: inherit; color: rgb(187, 187, 187); overflow: visible; appearance: button; cursor: pointer; background: rgba(224, 224, 224, 0.2); border-width: 0px; border-style: initial; border-color: initial; padding: 0px 0.5em; user-select: none; box-shadow: rgba(0, 0, 0, 0.2) 0px 2px 0px 0px; border-radius: 0.5em;">Copy</button></div></div></div><p>在这个示例中,我们使用了SQL参数化查询来删除年龄大于30的用户。</p><p class="maodian"></p><h2>使用Sequelize</h2><p>在前面的示例中,我们直接使用了原始的SQLite API来执行操作。然而,如果我们想要使用更高级的功能,如ORM(对象关系映射),那么可以考虑使用Sequelize。Sequelize是一个流行的Node.js库,提供了一种更简单、更强大的方式来操作SQLite数据库。</p><p>首先,我们需要使用npm安装<code>sequelize</code>模块和<code>sqlite3</code>模块:</p><div style="box-sizing: border-box; position: relative; color: rgb(33, 33, 33);"><pre style="box-sizing: border-box; overflow: auto; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: 20px;">npm install sequelize sqlite3</pre><div style="box-sizing: border-box; position: absolute; top: 0.3em; right: 0.2em; transition: opacity 0.3s ease-in-out 0s; opacity: 0;"><div><span style="box-sizing: border-box; color: rgb(187, 187, 187); font-size: 0.8em; padding: 0px 0.5em; background: rgba(224, 224, 224, 0.2); box-shadow: rgba(0, 0, 0, 0.2) 0px 2px 0px 0px; border-radius: 0.5em;">Bash</span></div><div><button style="margin: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 0.8em; font-family: inherit; color: rgb(187, 187, 187); overflow: visible; appearance: button; cursor: pointer; background: rgba(224, 224, 224, 0.2); border-width: 0px; border-style: initial; border-color: initial; padding: 0px 0.5em; user-select: none; box-shadow: rgba(0, 0, 0, 0.2) 0px 2px 0px 0px; border-radius: 0.5em;">Copy</button></div></div></div><p>然后,在我们的Electron主进程文件中,我们可以按照以下步骤来设置Sequelize:</p><div style="box-sizing: border-box; position: relative; color: rgb(33, 33, 33);"><pre style="box-sizing: border-box; overflow: auto; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: 20px;">const Sequelize = require('sequelize');const path = require('path');const dbPath = path.join(__dirname, './database/mydatabase.db');const sequelize = new Sequelize({
dialect: 'sqlite',
storage: dbPath,});// 定义模型const User = sequelize.define('User', {
name: {
type: Sequelize.STRING,
allowNull: false,
},
age: {
type: Sequelize.INTEGER,
allowNull: false,
},});// 同步模型到数据库sequelize.sync()
.then(() => {
console.log('Database and tables created!');
})
.catch((err) => {
console.error('Unable to connect to the database:', err);
});</pre><div style="box-sizing: border-box; position: absolute; top: 0.3em; right: 0.2em; transition: opacity 0.3s ease-in-out 0s; opacity: 0;"><div><span style="box-sizing: border-box; color: rgb(187, 187, 187); font-size: 0.8em; padding: 0px 0.5em; background: rgba(224, 224, 224, 0.2); box-shadow: rgba(0, 0, 0, 0.2) 0px 2px 0px 0px; border-radius: 0.5em;">JavaScript</span></div><div><button style="margin: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 0.8em; font-family: inherit; color: rgb(187, 187, 187); overflow: visible; appearance: button; cursor: pointer; background: rgba(224, 224, 224, 0.2); border-width: 0px; border-style: initial; border-color: initial; padding: 0px 0.5em; user-select: none; box-shadow: rgba(0, 0, 0, 0.2) 0px 2px 0px 0px; border-radius: 0.5em;">Copy</button></div></div></div><p>在这个示例中,我们首先引入了<code>sequelize</code>和<code>sqlite3</code>模块。然后,我们使用<code>path</code>模块构建了数据库的路径,并使用<code>new Sequelize()</code>方法创建了一个Sequelize实例。</p><p>接下来,我们定义了一个名为<code>User</code>的模型,并定义了两个属性:<code>name</code>和<code>age</code>。最后,我们使用<code>sequelize.sync()</code>方法将模型同步到数据库。</p><p>使用Sequelize,我们可以更方便地进行CRUD操作。以下是一些示例:</p><div style="box-sizing: border-box; position: relative; color: rgb(33, 33, 33);"><pre style="box-sizing: border-box; overflow: auto; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: 20px;">// 插入数据User.create({ name: 'John', age: 25 })
.then((user) => {
console.log(user.toJSON());
})
.catch((err) => {
console.error(err);
});// 查询数据User.findAll()
.then((users) => {
users.forEach((user) => {
console.log(user.toJSON());
});
})
.catch((err) => {
console.error(err);
});// 更新数据User.update({ age: 30 }, { where: { name: 'John' } })
.then((result) => {
console.log(result);
})
.catch((err) => {
console.error(err);
});// 删除数据User.destroy({ where: { age: { : 30 } } })
.then((result) => {
console.log(result);
})
.catch((err) => {
console.error(err);
});</pre><div style="box-sizing: border-box; position: absolute; top: 0.3em; right: 0.2em; transition: opacity 0.3s ease-in-out 0s; opacity: 0;"><div><span style="box-sizing: border-box; color: rgb(187, 187, 187); font-size: 0.8em; padding: 0px 0.5em; background: rgba(224, 224, 224, 0.2); box-shadow: rgba(0, 0, 0, 0.2) 0px 2px 0px 0px; border-radius: 0.5em;">JavaScript</span></div><div><button style="margin: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 0.8em; font-family: inherit; color: rgb(187, 187, 187); overflow: visible; appearance: button; cursor: pointer; background: rgba(224, 224, 224, 0.2); border-width: 0px; border-style: initial; border-color: initial; padding: 0px 0.5em; user-select: none; box-shadow: rgba(0, 0, 0, 0.2) 0px 2px 0px 0px; border-radius: 0.5em;">Copy</button></div></div></div><p>在这些示例中,我们通过调用模型的方法来执行CRUD操作。</p><p>以上就是在Electron中使用SQLite数据库的基本介绍。通过使用SQLite,我们可以在Electron应用程序中存储和检索数据,并使用Sequelize可以更方便地进行CRUD操作。</p><p><br/></p>
頁:
[1]