Updating Data with Raw SQL[an error occurred while processing this directive]

[an error occurred while processing this directive]

The raw SQL command UPDATE updates data in a database. An alternative is to use the Wolfram Language command SQLUpdate, described in "Updating Data".

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".

When updating data, the result of SQLExecute is an integer specifying the number of rows affected by the query.

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

Click for copyable input

As discussed in "Creating Tables with Raw SQL", the ADDRESSES and MAILER tables should be created.

Click for copyable input

This executes an SQL statement that updates a row in the MAILER table. This query updates the SENDMAILER column based on the value of USERNAME. Many update statements may be created using conditions that work with values in columns.

Click for copyable input

A SELECT statement verifies that the data has been changed in the table.

Click for copyable input

Using prepared statements, you can dynamically create SQL statements that update data within the database. You can combine this with a simple Wolfram Language function. This example updates the address for a particular user.

Click for copyable input
Click for copyable input

A SELECT statement verifies that the data has been changed in the table.

Click for copyable input

The same restrictions that apply to inserts also apply to updates. Thus, if you try to update an ADDRESS value to equal the ADDRESS value of another row, an error will be returned; this table requires them to be unique.

Click for copyable input

This deletes the tables and closes the connection.

Click for copyable input