HTML Web SQL Update

HTML Web SQL Update

In the following example, we will update a value (where id=2) in the table ('myTeam') under the database ('studentsDB').

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <button type="button" onclick="update()">Update</button> <div id="point"></div> <script> var x = document.getElementById('point'); var db = openDatabase('studentsDB', '1.0', 'Test DB', 2 * 1024 * 1024); // Insert db.transaction(function (tx) { tx.executeSql('CREATE TABLE IF NOT EXISTS myTeam (id unique, name)'); tx.executeSql('INSERT INTO myTeam (id, name) VALUES (1, "Brendan")'); tx.executeSql('INSERT INTO myTeam (id, name) VALUES (2, "Harley")'); }); function update(){ // Update db.transaction(function (tx) { tx.executeSql('update myTeam set name=? where id=?', ["Eich", 2]); }); // Retrieve db.transaction(function (tx) { tx.executeSql('SELECT * FROM myTeam', [], function (tx, data){ var len = data.rows.length, i; msg = "<p>You have: " + len + " members in your team</p>"; x.innerHTML += msg; for(i = 0; i < len; i++) x.innerHTML += (data.rows.item(i).name )+ "<br>"; }, null); }); } </script> </body> </html>

Update With Callback

In the following example, the callback function will be called after updating a value in the table.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <button type="button" onclick="update()">Update</button> <div id="point"></div> <script> var x = document.getElementById("point"); var db = openDatabase('studentsDB', '1.0', 'Test DB', 2 * 1024 * 1024); // Insert db.transaction(function (tx) { tx.executeSql('CREATE TABLE IF NOT EXISTS myTeam (id unique, name)'); tx.executeSql('INSERT INTO myTeam (id, name) VALUES (1, "Brendan")'); tx.executeSql('INSERT INTO myTeam (id, name) VALUES (2, "Harley")'); }); function update(){ // Update db.transaction(function (tx){ tx.executeSql('update myTeam set name=? where id=?', ["Eich", 2], function(transaction, result){ if(result) alert(result.rowsAffected+ " row(s) affected"); else alert("Something Went Wrong"); }); }); // Retrieve db.transaction(function (tx) { tx.executeSql('SELECT * FROM myTeam', [], function (tx, data) { var len = data.rows.length, i; msg = "<p>You have: " + len + " members in your team</p>"; x.innerHTML += msg; for(i = 0; i < len; i++) x.innerHTML += (data.rows.item(i).name )+ "<br>"; }, null); }); } </script> </body> </html>

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author