DATABASELINK 教程

Inserting Data

SQLInsert inserts data into a database. An alternative, using raw SQL, is described in "Inserting 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 SQLInsert is an integer specifying the number of rows affected by the query. For a single insert this will be 1, since you can only insert one row at a time. SQLInsert also supports a batch insert, as demonstrated in "Performance: Batch Operation".

SQLInsert[conn,table,{columns},{values},opts]insert data into a database
SQLInsert[conn,table,{columns},{{values}},opts]batch insert data into a database

Inserting data into a database.

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

In[1]:=
Click for copyable input

A new table, TEST, is created. The details of this command are described in "Creating Tables".

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

SQLInsert inserts data into this table.

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

SQLSelect verifies the data stored in the database.

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

Finally, a batch insert is carried out. The result is a list of the number of lines that are modified.

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

SQLSelect shows that there are now three rows in this table.

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

SQLInsert accepts one option.

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

Option of SQLInsert.

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[8]:=
Click for copyable input

The details of how Mathematica expressions are mapped to types stored in the database is discussed in "Data Type Mapping".

New to Mathematica? Find your learning path »
Have a question? Ask support »