如何使用电话间隙访问数据库

发布于 2025-01-08 15:36:46 字数 135 浏览 1 评论 0原文

我是 Android PhoneGap 的新手。我正在使用本机应用程序存储和检索数据。我不知道如何将检索到的数据从本机显示到phonegap(HTML)页面。

谁能指导我如何使用phonegap访问sqlite?

提前致谢。

I am new to android phonegap. i am storing and retrieving data using native application. i dont know how to display the retrieved data from native to phonegap(HTML)page.

can anyone pls guide me how to access sqlite with phonegap.?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

指尖上的星空 2025-01-15 15:36:46

您需要首先为 Phonegap 创建一个 Android 插件,通过它您将能够访问本机代码,从而访问本机数据库,如下所示

public class SqlitePlugin extends Plugin {

private static final String TAG = "SqlitePlugin";
private static final String CREATE_DB_ACTION = "createDatabase";
private static final String SHOW_DB_VALUES_ACTION = "showValues";

@Override
public PluginResult execute(String action, JSONArray data, String callbackId) {
    Log.i(TAG, "Plugin Called");
    PluginResult result = null;

    if (CREATE_DB_ACTION.equals(action)) {
        Log.d(TAG, "CREATE_DB_ACTION");
        DB _db = new DB(ctx);
        _db.insertValues();
    }
    else if (SHOW_DB_VALUES_ACTION.equals(action)) {
        Log.d(TAG, "SHOW_DB_VALUES_ACTION");

        JSONObject DBInfo = null;
        try {
            DBInfo = getDBValuesListing();

        } catch (JSONException e) {
            e.printStackTrace();
        }

        result = new PluginResult(Status.OK, DBInfo);

    }       
    else {
        result = new PluginResult(Status.INVALID_ACTION);
        Log.d(TAG, "Invalid action : " + action + " passed");
    }

    return result;
  }
}

然后创建一个 sqlite.js 文件,如下所示

    function SqlitePlugin() {
};

SqlitePlugin.prototype.createDatabase = function(successCallback, failCallback) {

return PhoneGap.exec(successCallback, failCallback, "SqlitePlugin",
        "createDatabase", [ null ]);
};

SqlitePlugin.prototype.showValues = function(params, successCallback, failCallback) {
return PhoneGap.exec(successCallback, failCallback, 'SqlitePlugin', 'showValues',
        [ params ]);
};

PhoneGap.addConstructor(function() {
PhoneGap.addPlugin("SqlitePlugin", new SqlitePlugin());
});

将此 sqlite.js 导入您的页面(index.html )然后最后使用这样的插件

function showValues() {
    window.plugins.SqlitePlugin.showValues('showValues',
            showValuesSuccessCallBack, showValuesFailCallBack);

}
function showValuesSuccessCallBack(e) {
    if (e.Rows.length > 0) {
        alert("Success");
        for (i = 0; i < e.Rows.length; i++) {
            alert("Id = " + e.Rows[i].id);
            alert("Number = " + e.Rows[i].number);
        }
    } else {
        alert("No values in Database");
    }
}

function showValuesFailCallBack(f) {
    alert("Failure");
}

让我知道这是否适合您

You need to first create a Android plugin for Phonegap through which you will be able to access the native code and hence the native DB like this

public class SqlitePlugin extends Plugin {

private static final String TAG = "SqlitePlugin";
private static final String CREATE_DB_ACTION = "createDatabase";
private static final String SHOW_DB_VALUES_ACTION = "showValues";

@Override
public PluginResult execute(String action, JSONArray data, String callbackId) {
    Log.i(TAG, "Plugin Called");
    PluginResult result = null;

    if (CREATE_DB_ACTION.equals(action)) {
        Log.d(TAG, "CREATE_DB_ACTION");
        DB _db = new DB(ctx);
        _db.insertValues();
    }
    else if (SHOW_DB_VALUES_ACTION.equals(action)) {
        Log.d(TAG, "SHOW_DB_VALUES_ACTION");

        JSONObject DBInfo = null;
        try {
            DBInfo = getDBValuesListing();

        } catch (JSONException e) {
            e.printStackTrace();
        }

        result = new PluginResult(Status.OK, DBInfo);

    }       
    else {
        result = new PluginResult(Status.INVALID_ACTION);
        Log.d(TAG, "Invalid action : " + action + " passed");
    }

    return result;
  }
}

After that Create a sqlite.js file like this

    function SqlitePlugin() {
};

SqlitePlugin.prototype.createDatabase = function(successCallback, failCallback) {

return PhoneGap.exec(successCallback, failCallback, "SqlitePlugin",
        "createDatabase", [ null ]);
};

SqlitePlugin.prototype.showValues = function(params, successCallback, failCallback) {
return PhoneGap.exec(successCallback, failCallback, 'SqlitePlugin', 'showValues',
        [ params ]);
};

PhoneGap.addConstructor(function() {
PhoneGap.addPlugin("SqlitePlugin", new SqlitePlugin());
});

Import this sqlite.js in your page(index.html) and then finally use the plugin like this

function showValues() {
    window.plugins.SqlitePlugin.showValues('showValues',
            showValuesSuccessCallBack, showValuesFailCallBack);

}
function showValuesSuccessCallBack(e) {
    if (e.Rows.length > 0) {
        alert("Success");
        for (i = 0; i < e.Rows.length; i++) {
            alert("Id = " + e.Rows[i].id);
            alert("Number = " + e.Rows[i].number);
        }
    } else {
        alert("No values in Database");
    }
}

function showValuesFailCallBack(f) {
    alert("Failure");
}

Let me know if this worked out for you

梦与时光遇 2025-01-15 15:36:46

编写一个phonegap插件将数据从native端传递到html(js)
http://wiki.phonegap.com/w/page/36753494/How%20to%20Create%20a%20PhoneGap%20Plugin%20for%20Android

Write an phonegap plugin to pass the data from native side to html (js)
http://wiki.phonegap.com/w/page/36753494/How%20to%20Create%20a%20PhoneGap%20Plugin%20for%20Android

关于从前 2025-01-15 15:36:46

好吧,您可能应该使用 HTML5 函数来存储和检索 sqlite 数据库中的数据。但是,如果您打算使用本机代码执行此操作,则应该查看我们的实现,该实现用于不支持 sqlite 的旧版 Android 设备。

https://github.com/cordova/cordova -android/blob/master/framework/assets/js/storage.js
https://github.com/cordova /cordova-android/blob/master/framework/src/com/phonegap/Storage.java

Well, you probably should use the HTML5 functions to store and retrieve data from a sqlite DB. However, if you are set on doing it with native code you should look at our implementation which was used for older Android devices that don't support sqlite.

https://github.com/cordova/cordova-android/blob/master/framework/assets/js/storage.js
https://github.com/cordova/cordova-android/blob/master/framework/src/com/phonegap/Storage.java

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文