Previous Next JavaScript must be enabled to correctly display this content

## Using API to update data

You can use the UPDATE SQL command in the Query request to update data.

To execute your query, you use the `NoSQLHandle.query()` API.

Download the full code ModifyData.java from the examples here.

    //Update data
    private static void updateRows(NoSQLHandle handle,String sqlstmt) throws Exception {
       QueryRequest queryRequest = new QueryRequest().setStatement(sqlstmt);
       handle.query(queryRequest);
       System.out.println("Updated table " + tableName);
    }

    /* update non-JSON data*/
    String upd_stmt ="UPDATE stream_acct SET account_expiry=\"2023-12-28T00:00:00.0Z\" WHERE acct_Id=3";
    updateRows(handle,upd_stmt);

Note:

Oracle NoSQL Database also supports user-defined row metadata. You can annotate actual row data with additional information in a JSON string using the `setRowMetadata()` method. To learn how it works, see Using row metadata in Write Operations.

To execute your query use the `borneo.NoSQLHandle.query()` method.

Download the full code ModifyData.py from the examples here.

    #update data
    def update_data(handle,sqlstmt):
       request = QueryRequest().set_statement(sqlstmt)
       result = handle.query(request)
       print('Data Updated in table: stream_acct')

    # update non-JSON data
    upd_stmt ='''UPDATE stream_acct SET account_expiry="2023-12-28T00:00:00.0Z" WHERE acct_Id=3'''
    update_data(handle,upd_stmt)

To execute a query use the `Client.Query` function.

Download the full code ModifyData.go from the examples here.

    //update data in the table
    func updateRows(client *nosqldb.Client, err error, tableName string, querystmt string)(){
       prepReq := &nosqldb.PrepareRequest{
    		Statement: querystmt,
       }
       prepRes, err := client.Prepare(prepReq)
       if err != nil {
          fmt.Printf("Prepare failed: %v\n", err)
          return
       }
       queryReq := &nosqldb.QueryRequest{
          PreparedStatement: &prepRes.PreparedStatement,   }
          var results []*types.MapValue
          for {
             queryRes, err := client.Query(queryReq)
      	if err != nil {
         	fmt.Printf("Upsert failed: %v\n", err)
         	return
      	}
      	res, err := queryRes.GetResults()
      	if err != nil {
         	fmt.Printf("GetResults() failed: %v\n", err)
         	return
      	}
      	results = append(results, res...)
      	if queryReq.IsDone() {
         	break
      	}
         }
         for i, r := range results {
            fmt.Printf("\t%d: %s\n", i+1, jsonutil.AsJSON(r.Map()))
         }
         fmt.Printf("Updated data in the table: \n")
    }

    updt_stmt := "UPDATE stream_acct SET account_expiry='2023-12-28T00:00:00.0Z' WHERE acct_Id=3"
    updateRows(client, err,tableName,updt_stmt)

To execute a query use `query` method.

JavaScript: Download the full code ModifyData.js from the examples here.

    /*updates data in the table*/
    async function updateData(handle,querystmt) {
       const opt = {};
       try {
          do {
             const result = await handle.query(querystmt, opt);
             opt.continuationKey = result.continuationKey;
          } while(opt.continuationKey);
       } catch(error) {
          console.error('  Error: ' + error.message);
       }
    }

TypeScript: Download the full code ModifyData.ts from the examples here.

    interface StreamInt {
       acct_Id: Integer;
       profile_name: String;
       account_expiry: TIMESTAMP;
       acct_data: JSON;
    }

    async function updateData(handle: NoSQLClient,querystmt: string) {
       const opt = {};
       try {
          do {
             const result = await handle.query<StreamInt>(querystmt, opt);
             for(let row of result.rows) {
                console.log('  %O', row);
             }
             opt.continuationKey = result.continuationKey;
          } while(opt.continuationKey);
       } catch(error) {
          console.error('  Error: ' + error.message);
       }
    }

    const updt_stmt = 'UPDATE stream_acct SET account_expiry="2023-12-28T00:00:00.0Z" WHERE acct_Id=3'

    await updateData(handle,updt_stmt);
    console.log("Data updated in the table");

You can use the UPDATE SQL command in the Query request to update data. To execute a query, you may call `QueryAsync` method or call `GetQueryAsyncEnumerable` method and iterate over the resulting async enumerable.

Download the full code ModifyData.cs from the examples here.

    private static async Task updateData(NoSQLClient client,String querystmt){
       var queryEnumerable = client.GetQueryAsyncEnumerable(querystmt);
    }

    private const string updt_stmt =
    @"UPDATE stream_acct SET account_expiry =""2023-12-28T00:00:00.0Z"" WHERE acct_Id=3";

    await updateData(client,updt_stmt);
    Console.WriteLine("Data updated in the table");