2

WebSQL has been deprecated but it is still useful for some applications at this time.

This is my table, I am using KnockoutJS:

<table class='producttable'>
<tr>
    <th>Name</th>
    <th>Description</th>
    <th>Price</th>
</tr>
<tbody data-bind="foreach: products">
<tr>
    <td><input data-bind='value: name' /></td>
    <td><input data-bind='value: description' /></td>
    <td><input data-bind='value: price' /> </td>
</tr>
</tbody>
</table>

I have been able to save/insert data into WebSQL like this:

function saveproduct(product) {
    db.transaction(function (tx) {
        tx.executeSql('INSERT INTO products (name, description, price) VALUES (?, ?, ?)',
        [product.name, product.description, product.price]);
    });
}

I am unsure on how to update one row of data in WebSQL.

I have tried this below, but it does not work. Am I heading in the right direction?

function updateproduct(product) {
    db.transaction(function (tx) {
        tx.executeSql('UPDATE products SET name=? WHERE id=?', [product.title]);
    });
}
bjb568
  • 11,089
  • 11
  • 50
  • 71
Ausername
  • 31
  • 1
  • 1
  • 5
  • Your `UPDATE` is almost correct, but you have two `?` placeholders and only one argument. – DCoder Sep 07 '14 at 17:41
  • It will work with ID.. Normally when you read ID it from hidden fields etc. it may be String so you need to parseInt() the id first before passing it as parameter. – Uma Shankar Goel Jul 21 '18 at 02:27

2 Answers2

1

Thank you, it works when 'UPDATE products SET description=?, price=? WHERE name=?', [product.description, product.price, product.name]); is implemented. I will need to tweak it so name is id instead.

Ausername
  • 31
  • 1
  • 1
  • 5
0

Indeed your code requires 2 variables to be mapped into the query, however I will suggest another approach, which helped me quite a lot.

You have to create a query string before you put it into request:

function updateproduct(product) {

let update_query = "UPDATE products SET name=" + product.title +  "WHERE id=" + product.title


    db.transaction(function (tx) {
        tx.executeSql(update_query);
    });
}

Hope it helps!