electron-vue跨平台桌面应用开发实战教程(十二)——集成加密版的sqlite3:sqlcipher

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/David1025/article/details/107945806

本文主要讲解集成及使用sqlcipher,一个可以加密的sqlite。sqlcipher官方npm地址:https://www.npmjs.com/package/@journeyapps/sqlcipher

由于和sqlite的功能一样,只是增加了加密的功能,所以具体安装方法请参照:https://blog.csdn.net/David1025/article/details/104540050,

1. 安装sqlcipher依赖

  1. npm install "@journeyapps/sqlcipher"

安装完成之后,需要再运行一下(否则会出现找不到sqlite3.node)

  1. npm install

2.使用

  1. var sqlite3 = require('@journeyapps/sqlcipher').verbose();
  2. var db = new sqlite3.Database('test.db');
  3. db.serialize(function() {
  4. // Required to open a database created with SQLCipher 3.x
  5. db.run("PRAGMA cipher_compatibility = 3");
  6. db.run("PRAGMA key = 'mysecret'");
  7. db.run("CREATE TABLE lorem (info TEXT)");
  8. var stmt = db.prepare("INSERT INTO lorem VALUES (?)");
  9. for (var i = 0; i < 10; i++) {
  10. stmt.run("Ipsum " + i);
  11. }
  12. stmt.finalize();
  13. db.each("SELECT rowid AS id, info FROM lorem", function(err, row) {
  14. console.log(row.id + ": " + row.info);
  15. });
  16. });
  17. db.close();

PRAGMA key = ‘mysecret’” 为设置的密钥

更多资源请关注(自增程序员)