Add/Update/Delete table data

Add

Kooboo table is a dynamic table as discussed in previous chapter.

To add data into database table

 <script engine="kscript">
  var table = k.database.getTable("tablename");
  var obj = {fieldone: "value one", fieldtwo: "value two"};
  table.add(obj);
 </script>
		

System _id

Every object return Kooboo database table has a system _id field, you can use it as a key to update or delete record. If you define a primary key for table, you can use the primary key field value. 

On every add, the _id field value will be return as well. 

 <script engine="kscript">
  var table = k.database.getTable("tablename");
  var obj = {fieldone: "value one", fieldtwo: "value two"};
  var returnid = table.add(obj);
 </script>

Update

To update the data, you need to provide the key of the data you want to update.  The key can be the system _id field or the primary key value if you define a primary key for your table. 

        <script engine="kscript">
        var table = k.database.getTable("tablename");
        var obj = {fieldone: "value one", fieldtwo: "value two"};
        var returnid = table.add(obj);
        obj.name = "new name";
        table.update(returnid, obj);
        </script>

or if you define and know your primary key. 

        <script engine="kscript">
        var table = k.database.getTable("tablename");
        var obj = {fieldone: "value one", fieldtwo: "newvalue"}; 
        table.update("myprimarykey", obj);
        </script>

Delete

The delete function require the primary key or system _id, same as update function.

     var table = k.database.getTable("tablename");
     table.delete(key);