1. Home
  2. Docs
  3. UniDB 1.2
  4. UniDB class
  5. Update

Update

The Update() method updates an existing Record in the Table. The method itself has no parameter, but it requires the use of the Data() method for specifying the new values for the Table’s columns (the omitted columns won’t be updated).

… .$Update$() …

Note

Even if in the SQL Update statement the use of a condition is not mandatory, for security reasons the Update() method of the UniDB system requires you to use the Where() method.


Data()

In the Update operation, the Data() method is used for specifying the new values of the Record’s columns (those you want to update). The method requires a list of the columns you want to set with their new value. For doing this, you have to use the Value() method exposed by each column:

… .$Data$( %Column%.$Value$( %value%, ^jsonEncode = false^ ), … ) …

value|object
A new value for this column.

The parameter is an object data type and can receive any kind of value. However, this value should be of the same type as the column’s type. For example, if the column is an int column, the value has to be an integer number.

The parameter supports SQL Functions (🢂 see the SQL Functions page for details).
__
jsonEncode|(bool)
If set to true, the value will be translated into a JSON string.

var TestDB = new UniDB.Test();
var myUsers = TestDB.GetTable_Users();

// SQL equivalent:
// UPDATE users SET username = "pluto" WHERE ...
_ = myUsers
    .Update()
    .Data(
          myUsers.C.username.Value("pluto"),
    )
    .Where( ... )
    .Run( ... )

Mathematical operations

In the Update operation, the Data() method supports the four basic mathematical operations: addition, subtraction, multiplication, and division. Instead of replacing a numeric value with a new one, you can recalculate that value. For activating this feature, you have to write a string containing the following symbols and the number involved in the calculation:

OPERATIONSYMBOLEXAMPLE
Addition{+}{+}10
The column’s value will be increased by 10.
Subtraction{-}{-]5
The column’s value will be decreased by 5.
Multiplication{*}{*}2
The column’s value will be multiplied by 2.
Division{/}{/}2
The column’s value will be divided by 2.
_ = myUsers
    .Update()
    .Data(
          myUsers.C.money.Value("{+}1200"),
    )
    .Where( ... )
    .Run( ... )

If you need to perform more complex mathematical operations, you can use the special Math() operation which allows you to recalculate a Column value with a formula of any complexity.


Run()

For the Update() operation, you must use override 3 of the Run() method.

var TestDB = new UniDB.Test();
var myUsers = TestDB.GetTable_Users();

// SQL equivalent:
// UPDATE users SET username = "pluto" WHERE id = 6
_ = myUsers
    .Update()
    .Data(
          myUsers.C.username.Value("pluto"),
    )
    .Where( myUsers.C.id.Equal(6) )
    .Run(
          (Info info) => 
          {
              if (info.isOK)
                 Debug.Log("I have updated " + info.affectedRows + " Records.");
              else
                 Debug.LogWarning("No record inserted!");
          }
    )

The Info parameter will return the following specific data for the Update operation:

affectedRowsintThe number of Records which has been updated.

NOTE
If 0 (zero) is returned, it doesn’t mean that an error occurred.
It simply means that no Records have been updated.