一颓鲜、思路歷程:
因為之前據(jù)說 SQLCipher 插件不能很好地實現(xiàn)加解密的功能,而且之后還要與語音模塊的C庫做接口铸敏,所以本想就此做一個 Cordova 插件來做一層統(tǒng)一的C接口封裝杆融,來統(tǒng)一調(diào)用其它C接口,以便輕松擴展后面的C庫柏副,調(diào)研后發(fā)現(xiàn)代價較高。這個 Cordova-Hello-JNI-Plugin 可能以后要是做的時候還可能會用到蚣录。
- 插件至少要做 Android 和 iOS 兩套割择,Android 還好說,因為貧窮包归,iOS 沒法測試。
- 還要寫什么回調(diào)函數(shù),煩得很
所以公壤,思忖了再三换可,決定還是先搭環(huán)境看看 SQLCipher 加密到底哪里不好使。
二厦幅、 Ionic & Cordova環(huán)境搭建
據(jù)一開始那次搭 Ionic 的環(huán)境已過去半年沾鳄,早已記不得,重新來吧确憨。
- 新建Ionic工程
ionic start sqlite-test2 blank
- 添加 Cordova 的 Android 平臺支持
ionic cordova platform add android
- 添加 Cordova 的 Crosswalk 支持
cordova plugin add cordova-plugin-crosswalk-webview
- 添加 Cordova 的 cordova-sqlcipher-adapter 插件支持
cordova plugin add cordova-sqlcipher-adapter
好的译荞,環(huán)境搭完了,值得一提的是休弃,在第一次安裝的時候會遇到一個報錯吞歼,原因是 cordova-android 必須得是6版本。第二次塔猾,就沒了??篙骡。
三、具體SQLite操作頁面
添加頁面
ionic generate page sqlite
-
Oh...不需要丈甸,我們在首頁上做就可以糯俗,以上是想記錄
generate
語句可以實現(xiàn)Generate pipes, components, pages, directives, providers, and tabs (ionic-angular >= 3.0.0)
(來自這里)的正規(guī)生成。以下為具體頁面內(nèi)容睦擂。
home.html<ion-header> <ion-navbar> <ion-title> Ionic Blank </ion-title> </ion-navbar> </ion-header> <ion-content padding> <button ion-button (click)="openDB()">Open DB</button> <ion-label color="primary">SQL:</ion-label> <ion-input #sql_stm placeholder="SQL Statement"></ion-input> <button ion-button color="secondary" clear (click)="sql_stm.value=''">clear</button> <button ion-button (click)="execSQL(sql_stm.value)" color="secondary">ExecSQL</button> <button ion-button (click)="closeDB()" color="danger">Close DB</button> <ion-input style="height: 200px" placeholder="Enter a description" [(ngModel)]="textArea"></ion-input> <button ion-button full color="dark" (click)="clear()">Clear</button> </ion-content>
home.ts
import { Component, OnInit } from '@angular/core'; import { NavController } from 'ionic-angular'; @Component({ selector: 'page-home', templateUrl: 'home.html' }) export class HomePage implements OnInit{ textArea: string; db = null; dbConfig = { name: 'demo.db', key: '1234', location: 'default' }; constructor(public navCtrl: NavController) { this.textArea = ""; } ngOnInit() { this.updateTextArea(JSON.stringify(Object.keys(window))); } selfTest() { document.addEventListener('deviceready', function() { window['sqlitePlugin'].selfTest(() => { this.updateTextArea('SELF test OK'); }); }); } echoTest() { document.addEventListener('deviceready', function() { window['sqlitePlugin'].echoTest(() => { this.updateTextArea('ECHO test OK'); }); }); } openDB() { document.addEventListener('deviceready', () => { this.db = (<any>window).sqlitePlugin.openDatabase(this.dbConfig); }); } execSQL(sql) { // const sql = 'INSERT "main"."db_test" VALUES (1);'; this.db.executeSql(sql, [], rs => { this.updateTextArea(JSON.stringify(rs)); }, error => { this.updateTextArea(JSON.stringify(error.message)); }); } updateTextArea(text) { this.textArea = this.textArea + '\n' + text; } closeDB() { this.db.close(); alert("Close DB"); } clear() { this.textArea = ""; } }
測試通過 PRAGMA rekey='****' 改變密碼語句可以使用得湘。
-
關(guān)于SQLite加速,參考《提升SQLite數(shù)據(jù)插入效率低顿仇、速度慢的方法》淘正。因為SQLCipher是JavaScript的封裝,并沒有提供全部的 C 導(dǎo)出函數(shù)夺欲,所以分析過后跪帝,只能從三個層面加速 SQL:
- 顯式開啟事務(wù),SQLCipher 天然提供
db.transaction
接口些阅。 - 批量提交 SQL 語句伞剑,SQLCipher 推薦使用
db.sqlBatch
接口。 - 寫同步(synchronous)市埋,使用 PRAGMA 語句 關(guān)閉寫同步:
PRAGMA synchronous = OFF;
- 顯式開啟事務(wù),SQLCipher 天然提供
參考資料:
- 開發(fā)自己的cordova插件
- Cordova-Hello-JNI-Plugin
- SQLCipher
- SQLCipher API
- NPM cordova-sqlcipher-adapter
- Github Cordova-sqlcipher-adapter
- How to use SQLCipher in Ionic project
- Github Ionic
- Ionic CLI commands
- Ionic Components
- Ionic+Cordova+crosswalk android開發(fā)
- webapp開發(fā)學習--Ionic+Cordova 環(huán)境搭建
- Not compatible with cordova-android 7.0.0 #304
- 提升SQLite數(shù)據(jù)插入效率低黎泣、速度慢的方法