什么是 html5 localstorage 数据库范围?
1.数据库的范围是什么?
2.什么时候销毁?
3.如何检查数据库是否存在?
var db = window.openDatabase("Database", "1.0", "MyApp", 900000);
db.transaction(populateDB, errorCB, successCB);
function populateDB(tx) {
tx.executeSql('DROP TABLE IF EXISTS PRICE2');
tx.executeSql('CREATE TABLE IF NOT EXISTS PRICE2 (id ,P2_ID,P2_1,P2_2,P2_3,P2_Title,P2_Type,P2_ToType,P2_Up,P2_UpType,PP2_PriceTo,PP2_PriceUp,image)');
var theResults = vData.results;
for(var i=1 ; i < theResults.length ; i++){
tx.executeSql('INSERT INTO PRICE2 (id ,P2_ID,P2_1,P2_2,P2_3,P2_Title,P2_Type,P2_ToType,P2_Up,P2_UpType,PP2_PriceTo,PP2_PriceUp,image) VALUES (' + theResults[i].ID + ', "' + theResults[i].P2_ID + '", "' + theResults[i].P2_1 + '", "' + theResults[i].P2_2 +'", "' + theResults[i].P2_3 +'", "' + theResults[i].P2_Title +'", "' + theResults[i].P2_Type +'", "' + theResults[i].P2_ToType +'", "' + theResults[i].P2_Up +'", "' + theResults[i].P2_UpType +'", "' + theResults[i].PP2_PriceTo +'", "' + theResults[i].PP2_PriceUp +'", "' + theResults[i].image +'")');
}
}
// Transaction error callback
//
function errorCB(tx, err) {
alert("Error processing SQL: "+err);
}
1.what is the scope of the db?
2.when it's destroyed?
3.how to check if the db exist?
var db = window.openDatabase("Database", "1.0", "MyApp", 900000);
db.transaction(populateDB, errorCB, successCB);
function populateDB(tx) {
tx.executeSql('DROP TABLE IF EXISTS PRICE2');
tx.executeSql('CREATE TABLE IF NOT EXISTS PRICE2 (id ,P2_ID,P2_1,P2_2,P2_3,P2_Title,P2_Type,P2_ToType,P2_Up,P2_UpType,PP2_PriceTo,PP2_PriceUp,image)');
var theResults = vData.results;
for(var i=1 ; i < theResults.length ; i++){
tx.executeSql('INSERT INTO PRICE2 (id ,P2_ID,P2_1,P2_2,P2_3,P2_Title,P2_Type,P2_ToType,P2_Up,P2_UpType,PP2_PriceTo,PP2_PriceUp,image) VALUES (' + theResults[i].ID + ', "' + theResults[i].P2_ID + '", "' + theResults[i].P2_1 + '", "' + theResults[i].P2_2 +'", "' + theResults[i].P2_3 +'", "' + theResults[i].P2_Title +'", "' + theResults[i].P2_Type +'", "' + theResults[i].P2_ToType +'", "' + theResults[i].P2_Up +'", "' + theResults[i].P2_UpType +'", "' + theResults[i].PP2_PriceTo +'", "' + theResults[i].PP2_PriceUp +'", "' + theResults[i].image +'")');
}
}
// Transaction error callback
//
function errorCB(tx, err) {
alert("Error processing SQL: "+err);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我曾经使用过 websql。这是答案。
问 - 数据库的范围是什么?
A - 我假设从“范围”来看,您的意思是数据库可以在哪里访问。对于您的扩展,如果您在background.html中创建数据库,则只能在background.html中访问它。您可以使用其他技术从 ext/app 的其他页面访问您的数据库,例如 message传递 API 和
chrome.extension.getBackgroundPage
(推荐)。问:什么时候被摧毁?
A - 它将在应用程序/扩展卸载/重新安装时被销毁。注意:更新 ext/app 不会对数据库产生任何影响。
问 - 如何检查数据库是否存在?
答 - 据我所知,没有必要“检查”。只需调用 window.openDatabase 如果数据库不存在,它将创建它,如果存在,它将跳过创建部分并将值添加到 db var 。
编辑:我还发现无法找到此类基本问题的答案令人沮丧。当 twitter/google groups/forums/html5rocks.com 评论失败时,我最终向 IRC 的人询问... :P 我发现 IRC 是最后一个,而且几乎总是一个成功的解决方案,可以快速、现场回答,而且没有在论坛网站等待答案:)
i have worked with websql. Here are the answers..
Q - what is the scope of the db?
A - i assume from 'scope' you mean where is the db accessible. For your extension if you create the db in background.html, it will only be accessible in background.html. You can the use other techniques to access your db from other pages of the ext/app like message passing API and
chrome.extension.getBackgroundPage
(recomended).Q - when it's destroyed?
A - It will be destroyed on app/ext uninstall/reinstall. NOTE: updating the ext/app will not have any effect on the db.
Q - how to check if the db exist?
A - from what i know, there is no need to 'check'. just call the
window.openDatabase
if the db does not exist, it will create it and if it does exist, it will skip the creating part and add the value todb
var.EDIT: I also found it frustrating to not be able to find answers to such basic questions. i ended up asking people at IRC when twitter/google groups/forums/html5rocks.com comments failed... :P i find IRC to be the last and almost always a successful resort for quick, on-the-spot answers and not having to wait for an answer in a forum site :)