【问题标题】:PhoneGap Local Storage WebSQL, IndexedDB, html5sql, lawnchairPhoneGap 本地存储 WebSQL、IndexedDB、html5sql、草坪椅
【发布时间】:2014-06-18 21:24:55
【问题描述】:

我正在尝试找出在我正在使用 PhoneGap 开发的跨平台应用程序中存储数据的最佳方式。The API instructions 建议使用 WebSQL,但是这将不再受支持,并且 IndexedDB 目前仅适用于 Windows / Blackberry。

many of the answers are really oldthis 这里有很多不同的问题,但我似乎找不到最流行的 js 库,用于通过现有数据库发布应用程序?(即一个好帮手) .js 来简化事情)。

我查看了 HTML5SQL,但文档非常稀疏,我不确定。

【问题讨论】:

    标签: sqlite cordova web-sql lawnchair


    【解决方案1】:

    我不知道这个问题是否会因为更多偏好而被标记,但如果有帮助,我会给我 2 美分。

    我已经能够使用带有 Phonegap/Cordova 的 SQLite 来发布带有现有数据库的应用程序。基本上我在 Android 中的做法是使用 lite4cordova SQLite 插件:

    https://github.com/lite4cordova/Cordova-SQLitePlugin

    在加载的本机代码中,我会检查默认目录以查看是否存在具有特定名称的数据库:

    try {
            File dbFile = getDatabasePath("data.db");
            Log.v("info", "dbfiledir: " + dbFile.getAbsolutePath());
            if (!dbFile.exists()) {
                this.copy("data.db", dbFile.getAbsolutePath());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    

    如果不存在,将数据库文件从assets文件夹复制到默认数据库目录:

    void copy(String file, String folder) throws IOException {
        File CheckDirectory;
        CheckDirectory = new File(folder);
    
        String parentPath = CheckDirectory.getParent();
    
        File filedir = new File(parentPath);
        //File file2 = new File(file);
        //Log.v("info", "filedir: " + file2.getAbsolutePath());
        if (!filedir.exists()) {
            if (!filedir.mkdirs()) {
                return;
            }
        }
    
        InputStream in = this.getApplicationContext().getAssets().open(file);
        File newfile = new File(folder);
        OutputStream out = new FileOutputStream(newfile);
    
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0)
            out.write(buf, 0, len);
        in.close();
        out.close();
    }
    

    现在当应用程序加载时,它只会在数据库不存在时复制。我可以简单地使用打开数据库(语法可能因插件而异):

    db = window.sqlitePlugin.openDatabase({
        name : "data"                
    });
    

    我很快就会在 iOS 端执行相同的逻辑,但我认为它应该同样简单。

    话虽如此,http://plugreg.com 上列出了一个插件,它提供了一个帮助库,用于使用 WebSQL 传送预填充的数据库:

    https://github.com/Smile-SA/cordova-plugin-websqldatabase-initializer

    我想在我的项目中使用 SQLite,所以我选择了加载时复制方法。祝你好运!

    【讨论】:

    • 不得不说这很有帮助,因为我也在寻找可以与 Cordova 一起提供的本地数据库。
    【解决方案2】:

    最新更新: New Cross Platform Cordova WebSQL plugin by MS Open Tech

    Microsoft Open Technologies is publishing the new open sourceWebSQL pluginfor Apache Cordova and PhoneGap. This plugin allows developers to integrate a persistent SQL-based local storage solution in their Cordova apps using the exact same JavaScript code across Android, iOS, Windows Phone and Windows Store.

    【讨论】:

    • 但如果您使用它,PhoneGap 开发者应用程序或 IE 中的浏览器内调试将无法工作。这会延长开发周期。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-18
    • 1970-01-01
    • 2011-12-14
    相关资源
    最近更新 更多