开发者

HTML5 - Load Web SQL DB from local file?

开发者 https://www.devze.com 2023-02-20 12:16 出处:网络
Let\'s use a gre开发者_如何学Cat demo as an example here . 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 positio

Let's use a gre开发者_如何学Cat demo as an example here .

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) { 

}


I think i found an answer to this old tread:

DEMO Here

Short sample code (provided by the website):

$(function(){
var demoRunning = false;

$("#startTest").click(function(){
    if(!demoRunning){
        $(this).addClass("running");
        $("#demoRunning").show();
        $("#results").text("running...");
        demoRunning = true;
        try {
            html5sql.openDatabase("demo", "Demo Database", 5*1024*1024);

            $.get('demo-statements.sql',function(sql){ //Your server created sticky notes database file
                var startTime = new Date();
                html5sql.process(
                    sql,
                    function(){ //Success
                        var endTime = new Date();
                        $("#results").text("Table with 11000 entries created in: " +
                                            ((endTime - startTime) / 1000) + "s");
                        $("#startTest").removeClass("running");
                        $("#demoRunning").hide();
                        demoRunning = false;
                    },
                    function(error, failingQuery){ //Failure
                        $("#results").text("Error: " + error.message);
                        $("#startTest").removeClass("running");
                        $("#demoRunning").hide();
                        demoRunning = false;
                    }
                );
            });

        } catch (error) {
            $("#results").text("Error: " + error.message);
            $("#startTest").removeClass("running");
            $("#demoRunning").hide();
            demoRunning = false;
        }
    }
})
});

ADDITIONAL INFORMATION

This only works in browsers (either desktop or mobile) that support the webDB standard


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

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.

Very very rough pseudo code:

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
}

However, if you're going to allow other people to look at your sticky notes, why bother with a local HTML5 database at all? Just store them on the server?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消