/**
*@Project: 超级奶爸之宝宝喂养记
*@Author: LionGIS@163.com
*@Date: 2014-06-20
*@Copyright: 代码开源,欢迎转载,但请保留版本信息.
*/
package com.superdad.weiyang.plugins;
import org.json.JSONArray;
import org.json.JSONObject;
import android.content.Context;
import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import android.os.Environment;
import android.os.StatFs;
import android.util.Base64;
import android.util.Log;
import android.content.ContentValues;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Hashtable;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class PGSQLitePlugin extends Plugin {
/** List Action */
private static final String ACTION_EXECUTE="backgroundExecuteSql";
private static final String ACTION_OPEN="open";
private static final String ACTION_CLOSE="close";
private static final String ACTION_INSERT="insert";
private static final String ACTION_DELETE="delete";
private static final String ACTION_UPDATE="update";
private static final String ACTION_QUERY="query";
private static final String ACTION_REMOVE="remove";
private static final String ACTION_BATCHEXECUTE="backgroundExecuteSqlBatch";
private static final String ACTION_TRANSACTION="transactionExecuteSqlBatch";
private static final String USE_INTERNAL="internal";
private static final String USE_EXTERNAL="external";
private Hashtable<String,SQLiteDatabase> openDbs = new Hashtable<String,SQLiteDatabase>();
@Override
public PluginResult execute(String action, JSONArray data, String callbackId) {
Log.d("PGSQLitePlugin", "Plugin Called");
PluginResult result = null;
if (action.equals(PGSQLitePlugin.ACTION_EXECUTE)) {
result = rawQuery(data);
}
else if (action.equals(PGSQLitePlugin.ACTION_TRANSACTION)) {
result = batchRawQuery(data, true);
}
else if (action.equals(PGSQLitePlugin.ACTION_INSERT)) {
result = insertQuery(data);
}
else if (action.equals(PGSQLitePlugin.ACTION_DELETE)) {
result = deleteQuery(data);
}
else if (action.equals(PGSQLitePlugin.ACTION_UPDATE)) {
result = updateQuery(data);
}
else if (action.equals(PGSQLitePlugin.ACTION_QUERY)) {
result = query(data);
}
else if (action.equals(PGSQLitePlugin.ACTION_OPEN)) {
result = openDatabese(data);
}
else if (action.equals(PGSQLitePlugin.ACTION_CLOSE)) {
result = closeDatabese(data);
}
else if (action.equals(PGSQLitePlugin.ACTION_REMOVE)) {
result = remove(data);
}
else if (action.equals(PGSQLitePlugin.ACTION_BATCHEXECUTE)) {
result = batchRawQuery(data);
}
else {
result = new PluginResult(PluginResult.Status.NO_RESULT);
Log.d("PGSQLitePlugin", "Invalid action : "+action+" passed");
}
return result;
}
private SQLiteDatabase getDb(String path){
SQLiteDatabase db = (SQLiteDatabase)openDbs.get(path);
return db;
}
private String getStringAt(JSONArray data, int position, String dret){
String ret = getStringAt(data, position);
return (ret == null) ? dret : ret;
}
private String getStringAt(JSONArray data, int position){
String ret = null;
try{
ret = data.getString(position);
//JSONArray convert JavaScript undefined|null to string "null", fix it
ret = ( ret.equals("null") ) ? null : ret;
}
catch(Exception er){};
return ret;
}
private JSONArray getJSONArrayAt(JSONArray data, int position){
JSONArray ret = null;
try{
ret = (JSONArray)data.get(position);
}
catch(Exception er){};
return ret;
}
private JSONObject getJSONObjectAt(JSONArray data, int position){
JSONObject ret = null;
try{
ret = (JSONObject)data.get(position);
}
catch(Exception er){};
return ret;
}
private PluginResult query(JSONArray data){
PluginResult result = null;
try {
Log.d("PGSQLitePlugin", "query");
String dbName = data.getString(0);
String tableName = data.getString(1);
JSONArray columns = getJSONArrayAt(data, 2);
String where = getStringAt(data, 3);
JSONArray whereArgs = getJSONArrayAt(data, 4);
String groupBy = getStringAt(data, 5);
String having = getStringAt(data, 6);
String orderBy = getStringAt(data, 7);
String limit = getStringAt(data, 8);
String[] _whereArgs = null;
if (whereArgs != null){
int vLen = whereArgs.length();
_whereArgs = new String[vLen];
for (int i = 0; i < vLen; i++){
_whereArgs[i] = whereArgs.getString(i);
}
}
String[] _columns = null;
if (columns != null){
int vLen = columns.length();
_columns = new String[vLen];
for (int i = 0; i < vLen; i++){
_columns[i] = columns.getString(i);
}
}
SQLiteDatabase db = getDb(dbName);
Cursor cs = db.query(tableName, _columns, where, _whereArgs, groupBy, having, orderBy, limit);
if (cs != null){
JSONObject res = new JSONObject();
JSONArray rows = new JSONArray();
if (cs.moveToFirst()) {
String[] names = cs.getColumnNames();
int namesCoint = names.length;
do {
JSONObject row = new JSONObject();
for (int i = 0; i < namesCoint; i++){
String name = names[i];
row.put(name, cs.getString(cs.getColumnIndex( name )));
}
rows.put( row );
} while (cs.moveToNext());
}
res.put("rows", rows);
cs.close();
Log.d("PGSQLitePlugin", "query::count="+rows.length());
result = new PluginResult(PluginResult.Status.OK, res);
}
else {
result = new PluginResult(PluginResult.Status.ERROR, "Error execute query");
}
} catch (Exception e) {
Log.e("PGSQLitePlugin", e.getMessage());
result = new PluginResult(PluginResult.Status.ERROR, e.getMessage());
}
return result;
}
private PluginResult updateQuery(JSONArray data){
PluginResult result = null;
try {
Log.d("PGSQLitePlugin", "updateQuery");
String dbName = data.getString(0);
String tableName = data.getString(1);
JSONObject values = (JSONObject)data.get(2);
String where = getStringAt(data, 3, "1");
JSONArray whereArgs = getJSONArrayAt(data, 4);
String[] _whereArgs = null;
if (whereArgs != null){
int vLen = whereArgs.length();
_whereArgs = new String[vLen];
for (int i = 0; i < vLen; i++){
_whereArgs[i] = whereArgs.getString(i);
}
}
JSONArray names = values.names();
int vLenVal = names.length();
ContentValues _values = new ContentValues();
for (int i = 0; i < vLenVal; i++){
String name = names.getString(i);
_values.put( name, values.getString( name ) );
}
SQLiteDatabase db = getDb(dbName);
long count = db.update(tableName, _values, where, _whereArgs);
result = new PluginResult(PluginResult.Status.OK, count);
Log.d("PGSQLitePlugin", "updateQuery::count=" + count);
} catch (Exception e) {
Log.e("PGSQLitePlugin", e.getMessage());
result = new PluginResult(PluginResult.Status.ERROR, e.getMessage());
}
return result;
}
private PluginResult deleteQuery(JSONArray data){
PluginResult result = null;
try {
Log.d("PGSQLitePlugin", "deleteQuery");
String dbName = data.getString(0);
String tableName = data.getString(1);
String where = getStringAt(data, 2);
JSONArray whereArgs = getJSONArrayAt(data, 3);
String[] _whereArgs = null;
if (whereArgs != null){
int vLen = whereArgs.length();
_whereArgs = new String[vLen];
for (int i = 0; i < vLen; i++){
_whereArgs[i] = whereArgs.getString(i);
}
}
SQLiteDatabase db = getDb(dbName);
long count = db.delete(tableName, where, _whereArgs);
result = new PluginResult(PluginResu
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
phonegap Android html5混合开发例子 (666个子文件)
BOOM 102B
clean 103B
config 35B
cordova 2KB
tizen.css 563KB
sencha-touch.css 250KB
base.css 233KB
cupertino-classic.css 208KB
bb10.css 207KB
wp.css 199KB
cupertino.css 185KB
mountainview.css 170KB
main.css 7KB
app.css 2KB
debug 103B
emulate 105B
.gitignore 292B
index.html 1KB
android-support-v4.jar 384KB
cordova-2.0.0.jar 175KB
appinfo.jar 2KB
PGSQLitePlugin.java 19KB
R.java 2KB
WeiYang.java 1KB
BuildConfig.java 162B
sencha-touch-all-debug.js 3.49MB
sencha-touch-all.js 927KB
sencha-touch-debug.js 528KB
cordova-2.0.0.js 177KB
sencha-touch.js 129KB
Component.js 95KB
Store.js 89KB
Model.js 62KB
DateExtras.js 60KB
List.js 56KB
Ext-more.js 53KB
ClassManager.js 51KB
Container.js 51KB
Panel.js 48KB
Loader.js 45KB
NodeInterface.js 44KB
Base.js 44KB
AbstractChart.js 40KB
DataView.js 39KB
Path.js 38KB
Connection.js 38KB
Application.js 38KB
HTML5.js 38KB
Scroller.js 35KB
Array.js 35KB
Series.js 34KB
Default.js 33KB
Observable.js 33KB
Collection.js 31KB
NestedList.js 30KB
Element.style.js 29KB
Canvas.js 29KB
Class.js 28KB
Reader.js 28KB
Controller.js 28KB
Axis.js 28KB
Axis.js 27KB
Sencha.js 26KB
AbstractMixedCollection.js 26KB
MessageBox.js 25KB
Sprite.js 24KB
Matrix.js 23KB
Input.js 23KB
Element.js 23KB
Carousel.js 23KB
Sql.js 22KB
SyncProxy.js 21KB
ComponentQuery.js 21KB
Ext.js 21KB
Button.js 20KB
Object.js 20KB
Sencha.js 20KB
SvgContext.js 20KB
Anim.js 19KB
Surface.js 18KB
JSON.js 18KB
Select.js 18KB
Function.js 18KB
Color.js 18KB
PanZoom.js 18KB
Bar.js 17KB
RemotingProvider.js 17KB
View.js 17KB
Picker.js 17KB
Time.js 17KB
Helper.js 16KB
Server.js 15KB
Selectable.js 15KB
Slider.js 15KB
Pie3DPart.js 15KB
Crosshair.js 15KB
Geolocation.js 15KB
Proxy.js 15KB
XTemplateCompiler.js 15KB
BelongsTo.js 15KB
共 666 条
- 1
- 2
- 3
- 4
- 5
- 6
- 7
资源评论
- kinghln2017-05-12真是坑,这么简单个例子浪费2个积分,根本没什么用
Rocco仔
- 粉丝: 3
- 资源: 24
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功