Updating Data

SQLUpdate modifies data in a database. An alternative, using raw SQL, is described in "Updating Data with Raw SQL".

If you find that the examples in this tutorial do not work as shown, you may need to install or restore the example database with the "DatabaseLink`DatabaseExamples`" package, as described in "Using the Example Databases".

The result of SQLUpdate is an integer specifying the number of rows affected by the query.

SQLUpdate[conn,table,{columns},{values},opts]update data in a database
SQLUpdate[conn,table,{columns},{values},condition,opts]update data in a database using a condition

Updating data in a database.

Here is an example that updates data. This loads DatabaseLink and connects to the demo database.

Click for copyable input

A new table, TEST, is created and data is inserted.

Click for copyable input

SQLSelect shows the values in the table.

Click for copyable input

SQLUpdate updates the elements in the database and SQLSelect shows the result.

Click for copyable input

Typically, it is useful to set a condition for an update, with the condition specifying which rows should be updated. (For more information on conditions, see "Selecting Data".) In the following example, another row is inserted into the database.

Click for copyable input

Here an update is given for rows for which the entry in the first column is less than 15.

Click for copyable input

SQLUpdate accepts one option.

option name
default value
"Timeout"Automaticset the timeout for a query

Option of SQLUpdate.

The option "Timeout" can be used to cancel a query if it takes too long to execute.

This drops the table and closes the connection.

Click for copyable input