PhoneGap本地存储WebSQL,IndexedDB,html5sql,lawnchair

时间:2014-02-07 13:31:41

标签: sqlite cordova web-sql lawnchair

我正在尝试找出在我使用PhoneGap进行开发的跨平台应用程序中存储数据的最佳方法。The API instructions建议使用WebSQL但不再支持此类,而IndexedDB目前仅适用于Windows /黑莓。

此处many of the answers are really oldthis有很多不同的问题,但我似乎无法找到最受欢迎的js库来运送具有现有数据库的应用程序?(即一个好帮手.js简化事情)。

我查看了HTML5SQL,但文档很稀疏,草坪椅我不确定。

2 个答案:

答案 0 :(得分: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,所以我选择了on load on load方法。祝你好运!

答案 1 :(得分:0)

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

Microsoft Open Technologies is publishing the new open source WebSQL plugin for 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.