2011-02-26 99 views
0

我已經通過本教程:http://www.mobilehtml5.com/post/401111526/tutorial-your-first-mobile-html5-app-offline-storageHTML 5脫機的SQLite數據庫

我一直在使用它來創建一個簡單的數據庫來記錄錢街機,與表中存儲的名稱和數量在每臺機器中。

我的問題是,我不知道最好的方法來輕鬆地編輯行? html5rocks.com給了我幾個想法,但沒有什麼合適的。

任何人都可以把我放在正確的軌道上嗎?我不知道正確的方法,是否有某種類型的遊標類型的做法?或者我應該有一個循環加載到輸入表單框中的值,然後更新這些?

任何幫助將是輝煌的,我已經包含在下面的代碼,所以你可以看到我在做什麼。

<!DOCTYPE html> 
<html> 
<head> 
<title>Machine Total Calculator</title> 
<script src="http://www.google.com/jsapi"></script> 
<script> 
    google.load("jquery", "1.4.1"); 
</script> 
<script> 

    var db = window.openDatabase("Machines", "", "Arcade Machines", 1024*1000); 

function insertMachine(name, amount) { 
    db.transaction(function(tx) { 
     tx.executeSql('INSERT INTO groupOne (name, amount) VALUES (?, ?)', [name,  amount]); 
    }); 
    } 

    function renderResults(tx, rs) { 
    e = $('#status'); 
    e.html(""); 
    for(var i=0; i < rs.rows.length; i++) { 
     r = rs.rows.item(i); 
     e.html(e.html() + 'id: ' + r['id'] + ', Name: ' + r['name'] + ', Amount: ' + r['amount'] + '<br>'); 
    } 
    } 


    function renderRecords(name) { 
    db.transaction(function(tx) { 

     tx.executeSql('SELECT * FROM groupOne', [], renderResults); 

    }); 
    } 


    $(document).ready(function() { 
    db.transaction(function(tx) { 
     tx.executeSql('CREATE TABLE IF NOT EXISTS groupOne(id INTEGER PRIMARY KEY, name TEXT, amount DECIMAL)', []); 
    }); 


$('#machine_form').submit(function() { 
insertMachine($('#name').val(), $('#amount').val()); 

    renderRecords(); 
    return false; 
}); 


renderRecords(); 
}); 
</script> 
</head> 
<body> 
<form method="get" id="machine_form"> 
    <div> 
    <input type="name" id="name" placeholder="Enter Machine Name" size="30"/> 
    <input type="number" current id="amount" placeholder="Amount" name="amount" size="15" /> 
    <input type="submit" value="Add Machines" /> 
    </div> 
</form> 
<div id="status"> 
</div> 
</body> 
</html> 

回答

0

據我瞭解你的問題:你必須使用相同的方式,例如,在PHP + MySQL中。您必須將整個記錄按值裝載到INPUTs值中,記住記錄的ID(例如,在隱藏的INPUT中)。在某些UPDATE按鈕的onclick事件處理程序中,您可以獲取這些值以及ID,並排列SQL UPDATE查詢(「UPDATE groupOne SET name = ...,value = ... WHERE id = ...」)

不,這個SQL中沒有「遊標方式」。只要記住該記錄的ID並在UPDATE中的WHERE子句中使用它。

+0

好的,所以我想添加一個按鈕,打印出哪些行通過ID更新功能,並添加編輯和更新內的能力?我試過這個,但是id不會傳遞給函數:e.html(e.html()+'id:'+ r ['id'] +',Name:'+ r ['name']] +',金額:'+ r ['金額'] +'e'+'
'); } – mark 2011-02-27 15:34:26