2010-03-12 95 views
0

我在这件事上打了我的头,我会很乐意提供任何帮助。我需要在数据库中存储另一个页面的上下文(字符串ID列表)。我打开一个包含艺术品列表的页面,并将该页面保存到数据库中的这些artowrks标识符。当我点击一件艺术作品时,我打开它的网页,但是我可以访问数据库以了解上下文并参考下一个和上一个作品。Javascript html5数据库循环中的数据库事务问题

这是我的代码检索CONTEX:

updateContext = function() { 
    alert("updating context"); 
    try { 
    mydb.transaction(
    function(transaction) { 
    transaction.executeSql("select artworks.number from artworks, collections where collections.id = artworks.section_id and collections.short_name in ('cro', 'cra', 'crp', 'crm');", [], contextDataHandler, errorHandler); 
    }); 
    } catch(e) { 
    alert(e.message); 
    } 
} 

的contextDatahandler功能,那么在结果迭代,并再次填写current_context表:

contextDataHandler = function(transaction, results) { 
    try { 
    mydb.transaction(
    function(transaction) { 
    transaction.executeSql("drop table current_context;", [], nullDataHandler, errorHandler); 
    transaction.executeSql("create table current_context(id String);", [], nullDataHandler, errorHandler); 
    } 
    ) 
    } 
    catch(e) { 
    alert(e.message); 
    } 

    var s = ""; 
    for (var i=0; i < results.rows.length; i++) { 
    var item = results.rows.item(0); 
    s += item['number'] + " "; 
    mydb.transaction(
    function(tx) { 
     tx.executeSql("insert into current_context(id) values (?);", [item['number']]); 
    } 
    ) 
    } 
    alert(s); 
} 

的结果是,我得到的current_context表删除,重新创建和填充,但所有行都填充了LAST艺术作品ID。查询检索艺术作品id工作,所以我认为这是一个交易问题,但我不知道在哪里。

感谢您的帮助

回答

1

我仍然不明白的地方我的代码是错误的,但我用解决:

contextDataHandler = function(transaction, results) { 
    try { 
     mydb.transaction(
      function(transaction) { 
       transaction.executeSql("drop table current_context;", [], nullDataHandler, errorHandler); 
       transaction.executeSql("create table current_context(id String);", [], nullDataHandler, errorHandler); 
       for(var i=0; i < results.rows.length; i++) { 
        var item = results.rows.item(i); 
        transaction.executeSql("insert into current_context(id) values (?);", [item['number']], nullDataHandler, errorHandler); 
       } 
      } 
     ) 
    } 
    catch(e) { 
     alert(e.message); 
    } 
} 
1

(假设事实,在for循环中,你总能得到results.rows .item(0),而非索引的所有项目,是一个错字粘贴在问题的代码),当

你可能会击中“声名狼藉循环问题”与变量绑定(由Robert NymanJames Padolsey(原始页面描述消失,所以链接t o Google缓存))。

+0

感谢您的信息 – 2010-03-15 00:08:38