Html HTML5 数据库存储 (SQL lite) - 几个问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2689939/
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 database storage (SQL lite) - few questions
提问by PathOfNeo
Hy there,
你好,
I can't find enough beginner resources on the web about HTML5 database storage usage examples (CRUD)
我在网上找不到足够的关于 HTML5 数据库存储使用示例 (CRUD) 的初学者资源
I'm opening(creating) my DB like this:
我正在像这样打开(创建)我的数据库:
var db;
$(document).ready(function()
{
try
{
if (!window.openDatabase) {
alert('Not Supported -> Please try with a WebKit Browser');
} else {
var shortName = 'mydatab';
var version = '1.0';
var displayName = 'User Settings Database';
var maxSize = 3072*1024; // = 3MB in bytes 65536
db = openDatabase(shortName, version, displayName, maxSize);
}
}
catch(e)
{
if (e == 2) {
alert("Invalid database version.");
} else {
alert("Unknown error "+e+".");
}return;
}
});
QUESTION 1: How many databases can i create and use on one domain? QUESTION 2. How to delete (drop) a database. -> i have not figured this out yet.
问题 1:我可以在一个域上创建和使用多少个数据库?问题 2. 如何删除(删除)数据库。-> 我还没有弄清楚这一点。
To create sql queries you use transaction:
要创建 sql 查询,请使用事务:
function nullDataHandler(transaction, results) { }
function createTables(db)
{
db.transaction(function (transaction)
{
//first query causes the transaction to (intentionally) fail if the table exists.
transaction.executeSql('CREATE TABLE people(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL DEFAULT "John Doe", shirt TEXT NOT NULL DEFAULT "Purple");', [], nullDataHandler, errorHandler);
});
}
QUESTION 3: How so is the above transaciton failed if a table exists? Is the nullDataHandler involved to do this? Where on the web is there documentation explaining the executeSql API? Arguments?
问题 3:如果存在表,上述事务如何失败?是否涉及 nullDataHandler 来执行此操作?网上哪里有解释 executeSql API 的文档?参数?
thx
谢谢
回答by Jeffery To
The spec you're looking for is Web SQL Database. A quick reading suggests:
您正在寻找的规范是Web SQL Database。快速阅读建议:
- There is no limit, although once your databases increase beyond a certain size (5MB seems to be the default), the browser will prompt the user to allow for more space.
- There is no way, in the current spec, to delete databases.
- The executeSql() function takes an optional error callback argument.
- 没有限制,尽管一旦您的数据库增加超过一定大小(5MB 似乎是默认值),浏览器会提示用户允许更多空间。
- 在当前规范中,无法删除数据库。
- executeSql() 函数采用可选的错误回调参数。
HTML5 Doctoralso has a good introduction.
HTML5 Doctor也有很好的介绍。
Going forward, though, I'd recommend looking at Indexed DB. Web SQL has essentially been abandoned since there is no standard for SQL / SQLite. Even Microsoft has endorsed Indexed DB. See Consensus emerges for key Web app standard.
不过,展望未来,我建议您查看Indexed DB。由于没有 SQL / SQLite 标准,Web SQL 基本上已被放弃。甚至微软也认可索引数据库。请参阅关键 Web 应用程序标准的共识出现。
回答by Praveen Vijayan
CREATE TABLE IF NOT EXISTS table_name
will create a table table_name
only if if does not exist.
table_name
仅当不存在时才会创建表。
回答by John Fowler
I found the following WebSQL tutorials helpful for basic CRUD operations, as they contained examples, and explained what the code was doing:
我发现以下 WebSQL 教程对基本 CRUD 操作很有帮助,因为它们包含示例,并解释了代码的作用:
And the following links for SequelSphere (an HTML5 JavaScript SQL Relational DatabaseAlternative to WebSQL that works in all browsers, storing data in LocalStorage and IndexedDB):
以及 SequelSphere 的以下链接(一种HTML5 JavaScript SQL 关系数据库替代 WebSQL,适用于所有浏览器,将数据存储在 LocalStorage 和 IndexedDB 中):
回答by i_a
Using PersistenceJS there is a persistence.reset API which will wipe the database clean. PersistenceJS Site
使用 PersistenceJS 有一个 persistence.reset API 可以清除数据库。 PersistenceJS 站点
For developing / testing purposes, you can view content and delete webSQL, IndexedDB, cookies, etc by searching for your domain name at this URL in Chrome:
出于开发/测试目的,您可以通过在 Chrome 中的以下 URL 搜索您的域名来查看内容并删除 webSQL、IndexedDB、cookie 等:
chrome://settings/cookies
There, you can delete all the storage for a domain or just certain local storage entities. Yes, the URL implies just 'cookies', but the interface at this URL includes all types of offline storage.
在那里,您可以删除域的所有存储或仅删除某些本地存储实体。是的,该 URL 仅表示“cookies”,但此 URL 上的界面包括所有类型的离线存储。
It would be great I think if the Chrome developer tools interface had the ability to right-click and delete a data storage entity in the Resources tab along with inspecting the content. But for now, all I know of is the settings/cookies URL.
我认为如果 Chrome 开发人员工具界面能够在资源选项卡中右键单击并删除数据存储实体以及检查内容,那就太好了。但就目前而言,我所知道的只是设置/cookies URL。
回答by Durai Amuthan.H
It is supported on iOS safari,chrome and some latest version of opera....it's not yet adopted by IE and Firefoxthat's it......what more one can ask than local db on browser which has relational db system...so u can query it easily and handle complex data....which is very tougher in key vale based systems..
它在 iOS safari、chrome 和一些最新版本的歌剧上受支持......它尚未被 IE 和 Firefox 所采用,就是这样......除了具有关系数据库系统的浏览器上的本地数据库之外,还有什么可以问的。 ..所以你可以很容易地查询它并处理复杂的数据......这在基于关键值的系统中非常困难......
I remember reading it even supports upto one gb.i am not sure....
我记得读过它甚至最多支持 1 GB。我不确定....
Note:
笔记:
1)I'd like to mention one point there is a IDE called Dashcode which let's u build web apps that looks like iOS native.even there also web SQL is used.
1)我想提一点,有一个名为 Dashcode 的 IDE,它让我们可以构建看起来像 iOS 本机的 Web 应用程序。甚至还使用了 Web SQL。
2)actually web SQL is a implementation of SQLiteon browsers.
2) 实际上web SQL 是 SQLite在浏览器上的实现。
3)SQLite is most prefered in both iOS and androidas db for native code..
3)SQLite在 iOS 和 android 中最受青睐,作为本机代码的数据库。
The drawbacks of SQLite:
SQLite 的缺点:
The Lack of concurrency supportbut which is not a problem in browser as it's gonna be used by single user at a time..this is a case also in mobile.
在并发支持不足,但它不是在浏览器中的问题,因为这会是由单个用户在time..this使用的情况下也移动。
Conclusions:
结论:
Web Sql is abandoned by w3that's a sad thing so we've to explore other options.
w3 放弃了 Web Sql,这是一件可悲的事情,因此我们必须探索其他选择。