4

第18课:JavaScript 引擎操作 SQLite 数据库

 3 years ago
source link: https://blog.csdn.net/nokiaguy/article/details/108729639
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

第18课:JavaScript 引擎操作 SQLite 数据库

尽管 Electron 应用可以利用 localStorage API 将数据以键值的形式保存在客户端,但 localStorage 并不适合存储大量的数据,而且 localStorage 是通过键值存储数据的,比较适合保存配置信息,而不是海量的结构化数据,如二维表。因此,要想在 Electron 客户端保存更复杂的数据而且便于检索,就需要使用真正的数据库,如 SQLite、MySQL。

由于 Electron 是基于 Node.js 的,因而 Node.js 支持的数据库,Electron 同样也支持。本节将会介绍如何在 Electron 中使用 SQLite 数据库,后面的文章会介绍如何在 Electron 中直接操作 MySQL 数据库。

SQLite 是一款轻量级桌面关系型数据库,点击这里详见官网

SQLite 支持多种接口,如 Java、C++、C、Python、JavaScript 等,Node.js 也支持多种方式操作 SQLite 数据库,其中最简单的就是使用 sql.js。sql.js 是一个 JavaScript 脚本文件,是直接从 C 语言版本的 SQLite 操作引擎转换过来的,因此文件尺寸比较大,大概有 2.7 MB,读者在此并不需要打开 sql.js 研究里面的代码,只要知道如何用即可。

由于 sql.js 是使用纯 JavaScript 编写的,因而兼容性非常好,在 Node.js 中也有非常好的表现。本节将会介绍 sql.js 的基本用法,也就是如何利用 sql.js 创建或打开 SQLite 数据库,以及数据库的基本操作(增、删、改、查)。

读者只需要将 sql.js 文件复制到 Electron 工


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK