要求:ee-core >= 1.2.8
支持sqlite数据库
基础
特点
- 大数据量: 0-1024GB(单库)
- 高性能
- 类mysql语法
安装
准备:需要python环境 (macos安装Python)
# 安装构建工具(新版本自带,旧版本ee < 2.1.6需安装)
npm i electron-rebuild -D
# (必须)重新构建适合当前操作系统的sqlite
npm run re-sqlite
注:ee < 2.1.6的版本需要在package.json的 scripts 对象加入
"scripts": {
"re-sqlite": "electron-rebuild -f -w better-sqlite3"
}
安装失败?查看:常见问题
数据文件位置
打包前:项目根目录
electron-egg/data/xxx.db
打包后:软件缓存目录
# windows (例子)
C:\Users\Administrator\AppData\Roaming\ee\data\xxx.db
# macOS (例子)
Users/apple/Library/Application Support/ee/data/xxx.db
# Linux (例子)
$XDG_CONFIG_HOME or ~/.config/ee/data/xxx.db
简单操作:增删改查
- 连接数据库 ``` ‘use strict’;
const Service = require(‘ee-core’).Service; // 框架提供的数据库对象 const Storage = require(‘ee-core’).Storage; const _ = require(‘lodash’);
/**
- 数据存储
@class */ class StorageService extends Service {
constructor (ctx) { super(ctx);
// sqlite数据库 let sqliteOptions = { driver: ‘sqlite’, default: {
timeout: 6000,
verbose: console.log // 打印sql语法
} } this.demoSqliteDB = Storage.JsonDB.connection(‘sqlite-demo.db’, sqliteOptions); }
}
module.exports = StorageService;
- options 说明
let sqliteOptions = { driver: ‘sqlite’, // 驱动:lowdb、sqlite default: { timeout: 6000, verbose: console.log // 打印sql语法 } }
default对象
default.readonly: false 以只读模式打开数据库连接 (默认: false).
default.fileMustExist: false 如果数据库不存在, 将引发错误,而不是创建新文件. 对于内存中、临时或只读数据库连接,忽略此选项 (默认: false).
default.timeout: 5000 在锁定的数据库上执行查询时,在引发 SQLITE_BUSY 错误之前等待的毫秒数 (默认: 5000).
default.verbose: null 提供一个函数,该函数使用数据库连接执行的每个SQL字符串进行调用 (默认: null).
default.nativeBinding: 如果您使用的是移动、转换或连接JS文件的复杂构建系统, better-sqlite3 可能无法定位其本机 C++ addon (better_sqlite3.node). 如果你遇到这样的错误, 您可以通过使用此选项提供 better_sqlite3.node (相对于当前工作目录).
- 检查表是否存在,如果不存在则创建
/*
检查并创建表 (sqlite) / async checkAndCreateTableSqlite(tableName = ‘’) { if (_.isEmpty(tableName)) { throw new Error(
table name is required
); } // 检查表是否存在 const userTable = this.demoSqliteDB.db.prepare(‘SELECT FROM sqlite_master WHERE type=? AND name = ?’); const result = userTable.get(‘table’, tableName); //console.log(‘result:’, result); if (result) { return; }// 创建表 const create_table_user = `CREATE TABLE ${tableName} (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name CHAR(50) NOT NULL,
age INT
);` this.demoSqliteDB.db.exec(create_table_user); } ```
- 增加数据 ``` /*
增 Test data (sqlite) */ async addTestDataSqlite(data) { //console.log(“add data:”, data);
let table = ‘user’; await this.checkAndCreateTableSqlite(table);
const insert = this.demoSqliteDB.db.prepare(
INSERT INTO ${table} (name, age) VALUES (@name, @age)
); insert.run(data);return true; } ```
- 删除数据 ``` /*
删 Test data (sqlite) */ async delTestDataSqlite(name = ‘’) { //console.log(“delete name:”, name);
let table = ‘user’; await this.checkAndCreateTableSqlite(table);
const delUser = this.demoSqliteDB.db.prepare(
DELETE FROM ${table} WHERE name = ?
); delUser.run(name);return true; } ```
- 修改数据 ``` /*
改 Test data (sqlite) */ async updateTestDataSqlite(name= ‘’, age = 0) { //console.log(“update :”, {name, age});
let table = ‘user’; await this.checkAndCreateTableSqlite(table);
const updateUser = this.demoSqliteDB.db.prepare(
UPDATE ${table} SET age = ? WHERE name = ?
); updateUser.run(age, name);return true; } ```
- 查数据 ``` /*
查 Test data (sqlite) */ async getTestDataSqlite(age = 0) { //console.log(“select :”, {age});
let table = ‘user’; await this.checkAndCreateTableSqlite(table);
const selectUser = this.demoSqliteDB.db.prepare(
SELECT * FROM ${table} WHERE age = @age
); const users = selectUser.all({age: age}); //console.log(“select users:”, users); return users; }
/*
all Test data (sqlite) */ async getAllTestDataSqlite() { //console.log(“select all user”);
let table = ‘user’; await this.checkAndCreateTableSqlite(table);
const selectAllUser = this.demoSqliteDB.db.prepare(
SELECT * FROM ${table}
); const allUser = selectAllUser.all(); //console.log(“select allUser:”, allUser); return allUser; } ```图形化数据(Navicat)
- 连接本地sqlite-demo.db文件
- 表数据
Storage对象API文档
https://www.yuque.com/u34495/mivcfg/vv5cmv