【问题标题】:what is html5 localstorage db scope?什么是 html5 localstorage db 范围?
【发布时间】:2011-08-04 20:24:08
【问题描述】:

1.db的范围是什么?

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);
}

【问题讨论】:

    标签: html local-storage


    【解决方案1】:

    我曾使用过 websql。以下是答案..

    Q - db 的范围是什么?

    A - 我从“范围”假设您的意思是数据库可访问的位置。对于您的扩展,如果您在 background.html 中创建数据库,则只能在 background.html 中访问它。您可以使用其他技术从 ext/app 的其他页面访问您的数据库,例如 message passing APIchrome.extension.getBackgroundPage (recomended)。

    Q - 什么时候销毁?

    A - 它将在 app/ext 卸载/重新安装时被销毁。注意:更新 ext/app 不会对 db 产生任何影响。

    Q - 如何检查数据库是否存在?

    A - 据我所知,没有必要“检查”。只需调用window.openDatabase,如果数据库不存在,它将创建它,如果它存在,它将跳过创建部分并将值添加到db var。

    编辑:我还发现无法找到这些基本问题的答案令人沮丧。当 twitter/google groups/forums/html5rocks.com cmets 失败时,我最终在 IRC 询问了人们......:P 我发现 IRC 是最后一个,几乎总是一个成功的手段,可以快速、现场回答,但没有在论坛站点中等待答案:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-11
      • 2020-10-18
      • 2017-04-22
      • 1970-01-01
      • 2023-03-17
      相关资源
      最近更新 更多