DATABASELINK TUTORIAL

Updating Data with Raw SQL

The raw SQL command UPDATE updates data in a database. An alternative is to use the Mathematica 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 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.

In[1]:=
Click for copyable input

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

In[3]:=
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.

In[7]:=
Click for copyable input
Out[7]=

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

In[8]:=
Click for copyable input
Out[8]=

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

In[9]:=
Click for copyable input
In[10]:=
Click for copyable input
Out[10]=

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

In[11]:=
Click for copyable input
Out[11]=

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.

In[12]:=
Click for copyable input
Out[12]=

This deletes the tables and closes the connection.

In[9]:=
Click for copyable input
New to Mathematica? Find your learning path »
Have a question? Ask support »