在PhoneGap應用程序中,我們可以利用一款名叫Cordova-SQLitePlugin的插件來方便的操作基於瀏覽器內置數據庫或獨立的SQLite數據庫文件,此插件的基本信息:
1.項目地址:https://github.com/brodysoft/Cordova-SQLitePlugin
2.項目討論組:https://groups.google.com/forum/#!forum/Cordova-SQLitePlugin
3.有用的參考文章:http://yeti.mtm.net.cn/?p=1437
以下是我利用Cordova3.5.0創建的Android工程利用此插件的過程:
1.創建工程testDb:
cordova create testSQLite com.me.testSQLite testDb
2.添加Android工程:
cd testSQLite cordova platform add android
3.編譯此工程:
cordova build
4.配置Cordova-SQLitePlugin插件:
從https://github.com/brodysoft/Cordova-SQLitePlugin下載下來的壓縮包中有以下內容:
www/SQLitePlugin.js:此文件要在.HTML中用到。
src/android/org/pgsqlite/SQLitePlugin.java:此文件由PhoneGap引用。
在我們的Android工程中的配置文件“platforms/android/res/xml/config.xml”中加入此插件的配置項:
<feature name="SQLitePlugin"> <param name="android-package" value="org.pgsqlite.SQLitePlugin" /> </feature>
然后把www/SQLitePlugin.js拷貝到Android工程中的www文件夾中,並且把src/android/org/pgsqlite/SQLitePlugin.java拷貝到Android工程中的
"plugins/org/pgsqlite.SQLitePlugin.java"。不出意外的話,插件就配置好了。
5.修改Android工程中www目錄下的index.html文件,用於測試看插件是否正常工作了,我的測試代碼如下:
<!DOCTYPE html> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <html> <head> <meta charset="utf-8" /> <meta name="format-detection" content="telephone=no" /> <!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 --> <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" /> <link rel="stylesheet" type="text/css" href="css/index.css" /> <meta name="msapplication-tap-highlight" content="no" /> <title>Hello World</title> </head> <body> <div class="app"> <h1>Apache Cordova</h1> <div id="deviceready" class="blink"> <p class="event listening">這里是我的測試內容</p> <p class="event received">Device is Ready,goooooooooooooool</p> </div> </div> <script type="text/javascript" src="cordova.js"></script> <script type="text/javascript" src="js/index.js"></script> <script type="text/javascript" charset="utf-8" src="SQLitePlugin.js"></script> <script type="text/javascript"> // Wait for Cordova to load document.addEventListener("deviceready", onDeviceReady, false); // Cordova is ready function onDeviceReady() { alert( 'hello world!' ); console.log( "准備要打開數據庫了!" ); var db = window.sqlitePlugin.openDatabase({name: "my.db"}); db.transaction(function(tx) { tx.executeSql('DROP TABLE IF EXISTS test_table'); tx.executeSql('CREATE TABLE IF NOT EXISTS test_table (id integer primary key, data text, data_num integer)'); console.log( "正在執行數據庫操作!" ); // demonstrate PRAGMA: db.executeSql("pragma table_info (test_table);", [], function(res) { console.log("PRAGMA res: " + JSON.stringify(res)); }); tx.executeSql("INSERT INTO test_table (data, data_num) VALUES (?,?)", ["test", 100], function(tx, res) { console.log("insertId: " + res.insertId + " -- probably 1"); console.log("rowsAffected: " + res.rowsAffected + " -- should be 1"); db.transaction(function(tx) { tx.executeSql("select count(id) as cnt from test_table;", [], function(tx, res) { console.log("res.rows.length: " + res.rows.length + " -- should be 1"); console.log("res.rows.item(0).cnt: " + res.rows.item(0).cnt + " -- should be 1"); }); }); }, function(e) { console.log("ERROR: " + e.message); }); }); console.log( "結束操作數據庫!" ); } </script> </body> </html>
6.然后再次編譯一下:
cordova build
用eclipse打開此Android工程,編譯,運行,在調試信息中可以看到操作數據庫的相應日志內容,就說明插件已經生效了。
這里默認的是使用瀏覽器中的內置數據庫,此插件也可以用於操作拷貝到PhoneGap應用程序中的獨立SQLite數據庫文件。
可以參考此文章:http://yeti.mtm.net.cn/?p=1437