HTML5 - 从本地文件加载Web SQL DB? [英] HTML5 - Load Web SQL DB from local file?

查看:81
本文介绍了HTML5 - 从本地文件加载Web SQL DB?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们以一个很棒的演示为例这里

Let's use a great demo as an example here .

假设我创建了5个便签作为管理员。我的浏览器有一个带有这5个便签的SQLite DB及其各自的位置和文本。然后,我将此DB文件导出到托管页面的本地服务器。然后让我们说另一台计算机上的用户加载此页面,默认情况下会看到我的5个便签;如何使页面从本地文件加载SQLite DB,例如/var/www/html/db_files/5-sticky-notes.db,以便最终用户可以与我的便签进行交互?

Let's say I create 5 sticky notes as an "administrator". My browser has a SQLite DB with these 5 sticky notes and their respective positions and text. I then export this DB file to the local server where the page is hosted. Let's then say that a "user" on another computer loads this page up and, by default, sees my 5 sticky notes; how do I make the page load a SQLite DB from a local file, e.g. /var/www/html/db_files/5-sticky-notes.db, so that end-users can interact with my sticky notes?

这是从个人浏览器加载最终用户数据库的代码:

This is the code for loading the end-user's database from their personal browser:

var db;

try {
    if (window.openDatabase) {
        db = openDatabase("5-sticky-notes", "1.0", "HTML5 Database API example", 200000);
        if (!db)
            alert("Failed to open the database on disk.  This is probably because the version was bad or there is not enough space left in this domain's quota");
    } else
        alert("Couldn't open the database.  Please try with a WebKit nightly with this feature enabled");
} catch(err) { 

}


推荐答案

在浏览器中无法原生地执行此操作,但我认为这是可能的。

There's no way to do this natively in the browser, but it is possible I reckon.

您已启动Ajax请求将数据从本地数据库发送到服务器,然后访问您网站的新用户也会有Ajax请求将数据从服务器下载到他们的本地数据库。

You'd have initiate an Ajax request to send the data from your local database to the server, then a new user visiting your website would also have an Ajax request to pull down the data from the server, into their local database.

非常粗略的伪代码:

var db;

try
{
    if (window.openDatabase)
    {
        db = openDatabase("5-sticky-notes", "1.0", "HTML5 Database API example", 200000);

        var stickyNotesInDatabase // some code to determine if sticky notes are in the users local database

        if(!stickyNotesInDatabase)
        {
            $.getJson('/GetStickyNotes', function(data)
            {
                // Load data into database from JSON 'data' variable
            });
        }
    }
    else
    {
        // Handle no database support
    }
}
catch(err)
{ 
    // Handle error
}

但是,如果你要允许其他人查看您的便利贴,为什么还要使用本地HTML5数据库呢?只需将它们存储在服务器上?

编辑:我还应该指出WebSQL是一个死亡标准,逐步淘汰将被IndexedDB取代。

I should also point out that WebSQL is a dieing standard, being phased out to be replaced with IndexedDB.

这篇关于HTML5 - 从本地文件加载Web SQL DB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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