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
PhoneGap,Cordova[3.5.0-0.2.6]:利用插件Cordova-SQLitePlugin来操作SQLite数据库的更多相关文章
- 【Android】13.0 第13章 创建和访问SQLite数据库—本章示例主界面
分类:C#.Android.VS2015: 创建日期:2016-02-26 一.简介 Android 内置了三种数据存取方式:SQLite数据库.文件.SharedPreferences. 这一章我们 ...
- Windows8下PhoneGap 4 + Android Studio 1.0 + VS2013配置指南
1.准备工作 安装JDK1.6+,设置环境变量 JAVA_HOME C:\Program Files\Java\jdk1.5.0_07 CLASSPATH .;%JAVA_HOME%\lib Path ...
- cordova+Android Studio 1.0+ionic+win7(转)
转自http://blog.csdn.net/fuyunww/article/details/42216125 目录(?)[-] 在项目目录下执行 a创建工程 b添加平台支持 c添加插件在Androi ...
- 如何在Cordova Android 7.0.0 以下版本集成最新插件 极光插件为例
前提 Cordova Android 7.0.0开始改变了项目安卓平台的架构.新建一个空项目分别添加Android 6.4.0 和 Android 7.0.0平台: cordova platform ...
- cordova crosswalk android 7.0 问题
带有crosswalk的cordova app 在Android7.0会闪退问题,为什么要crosswalk,我的回答是,还tmd不是要兼容5.0以下系统(4.4,4.2,4.0),这里省略100 ...
- 第三步 Cordova 3.0(及以上版本) 添加插件
1.使用命令生成项目 例:cordova create jy110 com.example.jy110 jy110 2.使用命令添加插件(如果报错,可能是网络问题,可以多试几次,直到成功) 例:cor ...
- [Phonegap+Sencha Touch] 移动开发76 让cordova app訪问远端站点也能调用cordova插件功能
原文链接:http://blog.csdn.net/lovelyelfpop/article/details/50735395 我相信.应该会有一些cordova开发人员想过实现以下这种app: 使用 ...
- 解决android sdk 运行出现 could not install *smartsocket* listener: cannot bind to 127.0.0.1:5037:的问题
ionic3项目,在添加android平台后,cordova run android 出现 以下问题: error: could not install *smartsocket* listener: ...
- Castle Core 4.0.0 alpha001发布
时隔一年多以后Castle 项目又开始活跃,最近刚发布了Castle Core 4.0.0 的alpha版本, https://github.com/castleproject/Core/releas ...
随机推荐
- Photoshop:模拟钢笔压力
预期效果: 方法: 按B选择画笔工具,设置"钢笔压力" 路径描边:选择路径,右击路径,选择“描边路径” 或在路径面板,选择: 即可得到预期效果!
- 如何在Android应用程序中使用传感器模拟器SensorSimulator
原文地址; 如何在Android应用程序中使用传感器模拟器 - 移动平台应用软件开发技术 - 博客频道 - CSDN.NET http://blog.csdn.net/pku_android/arti ...
- jQuery通知插件 -- noty
noty是一个jQuery的通知(信息提示)插件,灵活轻便,是一个非常棒的用于替代传统提示对话框的插件. 当前最新版本为2.1.0: 从https://github.com/needim/noty 可 ...
- java.util.regex.PatternSyntaxException: Unclosed character class near index解决办法
使用str.split("[")时,出现java.util.regex.PatternSyntaxException: Unclosed character class near ...
- JCIFS读取远程服务器文件过慢的解决方法
JCIFS读取远程服务器文件过慢的解决方法 发表于3年前(2013-07-12 11:23) 阅读(1174) | 评论(0) // 我要收藏"; var favor_del = &qu ...
- 自己动手实现STL 02:构造析构的基本工具construct()和destroy()(stl_construct.h)
一.前言 上一篇,我先完成了对内存配置器的实现.然而后面在内存上的算法还依赖于两个全局函数,construct()和destroy(),前者负责在指定的内存上调用对象的构造函数,在内存上构造出对象.后 ...
- bzoj4048 3928
羞耻,分组赛上考的,竟然没想出来, 对坐标离散化后区间dp即可,竟然还双倍经验 ; ..,..] of longint; v:..] of longint; a,b,h:..] of longint; ...
- Qt之进程间通信(共享内存)
简述 上一节中,我们分享下如何利用Windows消息机制来进行不同进程间的通信.但是有很多局限性,比如:不能跨平台,而且必须两个进程同时存在才可以,要么进程A发了消息谁接收呢? 下面我们来分享另外一种 ...
- UVa 11105 (筛法) Semi-prime H-numbers
题意: 你现在来到了一个所有的数都模4余1的世界,也就是除了这种数没有其他的数了. 然而素数的定义依然没变,如果一个数不能写成两个非1数字的乘积,则它是素数. 比如,在这里5就变成了最小的素数. 两个 ...
- BZOJ 2194 快速傅里叶之二
fft. #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> ...