Javascript HTML5 本地存储和 SQL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1878256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTML5 localStorage & SQL
提问by jldupont
I understand that HTML5 "localStorage" is a key:value storebut I am wondering if there is a Javascript library available that offers a more SQL-ishAPI?
我知道 HTML5“localStorage”是一个键:值存储,但我想知道是否有可用的 Javascript 库提供更多SQL-ishAPI?
采纳答案by Kevin Hakanson
Check out Will HTML5 be SQL-free?and DOM Storage: a Cure for the Common Cookiefor some links and opinions.
查看HTML5是否将不含 SQL?和DOM Storage: A Cure for the Common Cookiefor some links and views。
回答by N 1.1
W3C Database specificationsays:
User agents must implement the SQL dialectsupported by Sqlite 3.6.19.
用户代理必须实现Sqlite 3.6.19 支持的SQL 方言。
As of now, at leastGoogle Chrome supports SQL dialect. I have checkedmyself.
到目前为止,至少谷歌浏览器支持 SQL 方言。我已经检查过自己了。
回答by Ionut Popa
You should use HTML5 database storage (it supports SQL through transactions). a tutorial here: http://www.html5rocks.com/tutorials/webdatabase/todo/
您应该使用 HTML5 数据库存储(它通过事务支持 SQL)。这里的教程:http: //www.html5rocks.com/tutorials/webdatabase/todo/
回答by Kavin Mehta
回答by agershun
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.
你可以试试Alasql。它支持标准的 SQL 语言并将数据保存在内存或 localStorage 中。有几种方法,如何将 Alasql 与 localStorage 一起使用。您可以在下面看到如何创建名为“Atlas”的 localStorage 数据库,将其作为“MyAtlas”附加到 Alasql,然后您就可以像处理任何其他数据库一样处理表。默认情况下,Alasql 使用 AUTOCOMMIT ON 模式,因此它在每个 SQL 语句之后将数据保存到 localStorage。
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.
在jsFiddle 中使用此示例。运行此示例两三次(或重新加载页面),您将看到表格中的行数将如何增长。
回答by Matt
HTML5 local database storage comes with a SQL interface by default, if I'm not mistaken
HTML5 本地数据库存储默认自带 SQL 接口,如果我没记错的话
Here is a Webkit post with some examples: http://webkit.org/blog/126/webkit-does-html5-client-side-database-storage/
这是一个带有一些示例的 Webkit 帖子: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.
目前,Chrome 强制您使用 Gears,它略有不同,但仍基于 SQL。但是,未来版本的 Chrome 将遵循 HTML5 规范。

