本文主要讲解集成及使用sqlcipher,一个可以加密的sqlite。sqlcipher官方npm地址:https://www.npmjs.com/package/@journeyapps/sqlcipher
由于和sqlite的功能一样,只是增加了加密的功能,所以具体安装方法请参照:https://my.oschina.net/david1025/blog/3182941
1. 安装sqlcipher依赖
npm install "@journeyapps/sqlcipher"
安装完成之后,需要再运行一下(否则会出现找不到sqlite3.node)
npm install
2.使用
var sqlite3 = require('@journeyapps/sqlcipher').verbose();
var db = new sqlite3.Database('test.db');
db.serialize(function() {
// Required to open a database created with SQLCipher 3.x
db.run("PRAGMA cipher_compatibility = 3");
db.run("PRAGMA key = 'mysecret'");
db.run("CREATE TABLE lorem (info TEXT)");
var stmt = db.prepare("INSERT INTO lorem VALUES (?)");
for (var i = 0; i < 10; i++) {
stmt.run("Ipsum " + i);
}
stmt.finalize();
db.each("SELECT rowid AS id, info FROM lorem", function(err, row) {
console.log(row.id + ": " + row.info);
});
});
db.close();
PRAGMA key = 'mysecret'" 为设置的密钥
更多资源请关注(自增程序员)
来源:oschina
链接:https://my.oschina.net/david1025/blog/4486099