WebSQL为PhoneGap的应用 [英] WebSQL for PhoneGap Application

查看:95
本文介绍了WebSQL为PhoneGap的应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找使用WebSQL将数据存储在一个Android PhoneGap的应用程序,但不知道从哪里开始。有没有必要从网上检索数据的应用程序脱机,我想有一个可以查询一个prepopulated数据库。几个问题: 如何创建一个pre填充的数据库? (它应该是什么样的文件格式) 如果有两个因素是可以改变的,我如何才能从数据库中的这些因素,使用SQL相结合的结果。 请问,如果用户清除在Android设备上从设置菜单中的应用程序数据的数据库被消灭? 感谢您的帮助。谢谢

I'm looking to use a WebSQL to store data in an Android PhoneGap app but dunno where to start. There is no need to retrieve data from the web as the application is offline, and I want to have a prepopulated database that can be queried. A few questions: How do you create a pre-populated database? (what kind of file format should it be) If there are two factors can be changed, how do I get the result of a combination of these factors from the database using SQL. Will the database be wiped if the user clears app data from the Settings menu on an Android device? Grateful for any help. thanks

推荐答案

1)您可以借助工具例如 SQLite的经理 或你已经在你的应用程序的数据库,你可以直接得到数据库文件。

Prepopulate SQLite DataBase in PhoneGap Application

1) You can create a basic database for app with the help of tools like SQLite Manager or if you already have a database in your app you can directly get the database files.

2)然后,你需要通过命令行安装科尔多瓦/ PhoneGap的SQLitePlugin。 (科尔多瓦/ PhoneGap的SQLitePlugin

2) Then you need to install Cordova/PhoneGap SQLitePlugin by CLI . (Cordova/PhoneGap SQLitePlugin)

cordova plugin add https://github.com/brodysoft/Cordova-SQLitePlugin

3)ADDD按照你的HTML的脚本文件。

3) Addd following script file in your html.

<script src="plugins/com.brodysoft.sqlitePlugin/www/SQLitePlugin.js"></script>

在资源文件夹

4)如果你正在为模拟器然后复制使用命令提示符的databas,我已经加入myDB.sqlite然后运行。

4) If you are working for emulator then copy your databas using command prompt, I have added myDB.sqlite in asset folder then run .

adb push myDB.sqlite /data/data/com.my_app.my_app/databases/myDB.sqlite

5)的设备,只是把他们捆绑的应用程序,在Android的情况下,资产的文件夹。你需要myDB.sqlite和file__0 / 0000000000000001.sqlite文件。

5) For device, just put them in to bundle of app that is assets folder in case of Android . you need myDB.sqlite and file__0/0000000000000001.sqlite files.

6)现在,你必须将文件复制在应用程序的应用程序第一次启动时的天然位置,并确保这些文件应你的第一个SQLite的查询之前复制。要复制这些文件,您可以使用基于您的环境下code段。 JAVA(安卓)。 更多...

6) Now you have to copy the files at the native location of application at the time of first boot of application, and make sure that the files should copy before your very first SQLite query. To copy these files you can use the following code snippet based on your environment. JAVA (Android). More...

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.init();
        try {
            String pName = this.getClass().getPackage().getName();
            this.copy("myDB.sqlite", "/data/data/" + pName + "/databases/");
            this.copy("0000000000000001.sqlite", "/data/data/" + pName + "/app_database/file__0/");
        } catch (IOException e) {
            e.printStackTrace();
        }

        super.loadUrl(Config.getStartUrl());
    }

    void copy(String file, String folder) throws IOException {

        File CheckDirectory;
        CheckDirectory = new File(folder);
        if (!CheckDirectory.exists()) {
            CheckDirectory.mkdir();
        }

        InputStream in = getApplicationContext().getAssets().open(file);
        OutputStream out = new FileOutputStream(folder + file);
        // Transfer bytes from in to out
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) out.write(buf, 0, len); in .close();
        out.close();

    }

7),然后在你的js文件中添加代码段。

7) Then add snippet in your js file.

    $(document).ready(function(e){      
        document.addEventListener("deviceready", dbConnection, false);            
    }) 

    function dbConnection(){

        db = window.sqlitePlugin.openDatabase("myDB.sqlite", "1.0", "DB", 2000000);
        db.transaction(function(tx) {  
            tx.executeSql("SELECT * from table", [], function(tx, res) {                           
                for(var i=0; i<res.rows.length; i++){                        ;
                    console.log("RESULT:" + res.rows.item(i)['field_name']);
                }            
            });
        });
    }

<一个href="http://stackoverflow.com/questions/26629244/phonegap3-4-0-android-app-with-sqlite-its-working-fine-in-emulator-but-not-in?noredirect=1#comment41867305_26629244">More详细...

More Detail...

这篇关于WebSQL为PhoneGap的应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆