English 中文(简体)
HTML5 localStorage & SQL
原标题:

I understand that HTML5 "localStorage" is a key:value store but I am wondering if there is a Javascript library available that offers a more SQL-ish API?

最佳回答

Check out Will HTML5 be SQL-free? and DOM Storage: a Cure for the Common Cookie for some links and opinions.

问题回答

W3C Database specification says:

User agents must implement the SQL dialect supported by Sqlite 3.6.19.

As of now, at least Google Chrome supports SQL dialect. I have checked myself.

You should use HTML5 database storage (it supports SQL through transactions). a tutorial here: http://www.html5rocks.com/tutorials/webdatabase/todo/

HTML5 local database storage comes with a SQL interface by default, if I m not mistaken

Here is a Webkit post with some examples: http://webkit.org/blog/126/webkit-does-html5-client-side-database-storage/

Currently, Chrome forces you to use Gears, which is slightly different, but still SQL-based. Future versions of Chrome will follow the HTML5 spec, however.

You can try Alasql. It supports standard SQL language and keeps data in memory or localStorage. There are sevelar ways, how to use Alasql with localStorage. Below you can see how to create localStorage database with name "Atlas", attach it to Alasql as "MyAtlas", then you can work with tables like any other database. By default, Alasql uses AUTOCOMMIT ON mode, so it saves data to localStorage after each SQL statement.

This is a sample:

alasql( CREATE localStorage DATABASE IF NOT EXISTS Atlas );
alasql( ATTACH localStorage DATABASE Atlas AS MyAtlas );
alasql( CREATE TABLE IF NOT EXISTS MyAtlas.City (city string, population number) );
alasql( SELECT * INTO MyAtlas.City FROM ? ,[[{city: Vienna , population:1731000}, 
    {city: Budapest , population:1728000}]]);
var res = alasql( SELECT * FROM MyAtlas.City );

Play with this sample in jsFiddle. Run this sample two or three times (or reload page), and you will see, how the number of lines will grow in the table.





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签