DATABASELINK 教程

Deleting Data

SQLDelete deletes data from a database. An alternative, using raw SQL, is described in "Deleting 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 package, as described in "Using the Example Databases".

The result of SQLDelete is an integer specifying the number of rows affected by the query. Thus, if three rows are removed, the result is three, and if no rows are removed, the result is zero.

SQLDelete[table]delete data from a database
SQLDelete[table,condition]delete data from a database using a condition

Deleting data from a database.

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

In[1]:=
Click for copyable input

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

In[3]:=
Click for copyable input
Out[5]=

The following deletes all the data from the table. Two rows were deleted, and the result is two.

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

SQLSelect verifies that all the data has been removed from the table.

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

This restores the data in the database.

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

Here, a condition is used in the SQLDelete command, so that only rows for which the entry in the first column is greater than 15 are deleted. This deletes one row, and hence the result is one.

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

SQLSelect verifies that one row was removed from the table.

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

SQLDelete accepts one option.

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

Option of SQLDelete.

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

This drops the table and closes the connection.

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