PhoneGap,Cordova[3.5.0-0.2.6]:利用插件Cordova-SQLitePlugin来操作SQLite数据库

在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

 

转载于:https://www.cnblogs.com/dycsoft/p/3898254.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值