This is documentation for Mathematica 6, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)
DatabaseLink Tutorial

Examples

Command Cache

This example shows how to use a private database to store Mathematica commands and query the data from Java and Mathematica.
If you find that the examples in this section do not work as shown, you may need to install or restore the example database with the DatabaseExamples` package, as described in "Using the Example Databases".
The example code is loaded from the Examples subdirectory of DatabaseLink.
In[1]:=
Click for copyable input
The command cache allows you to store Mathematica expressions as typeset box expressions data in a database. StoreCommand is used to store the boxes in the database.
In[2]:=
Click for copyable input
Out[2]=
The data can then be retrieved from the database using CommandCache[].
In[3]:=
Click for copyable input
Out[3]=
Several attributes of each command are stored in the database. Each command is given an ID, generated when it is stored. The expression is stored as a string formatted with FullForm; this allows it to be reused in Mathematica. In addition, an image of the expression is saved as well as the time at which the data was stored. GetCommandAttributes can be used to get the attribute names. These can be used to filter the data returned.
In[4]:=
Click for copyable input
Out[4]=
In[5]:=
Click for copyable input
Out[5]=
The following example finds all results that contain Power.
In[6]:=
Click for copyable input
Out[6]=
In[7]:=
Click for copyable input
Out[7]=
In[8]:=
Click for copyable input
Out[8]=
A command can also be retrieved using its ID.
In[9]:=
Click for copyable input
Out[9]=
Another feature of this package is a Java GUI you can use to browse the data. It provides functionality for managing the data and pasting the data into a notebook.
In[10]:=
Click for copyable input
Out[10]=
The GUI is automatically updated when new commands are added to the database.
In[11]:=
Click for copyable input
Likewise, the GUI is automatically updated when commands are removed. ClearCommandCache can be used to remove all the data in the command cache.
In[12]:=
Click for copyable input
Out[12]=

Graph Database

This example shows a database that stores material generated by Mathematica. Here the material involves graphs and a number of properties of these graphs. Even though the number of graphs is not extremely large, generating the properties of these graphs can take a significant amount of time. This demonstrates the value of a database for persistent storage of the results of computations.

Using the Graph Database

To use the graph database you need to load the package.
In[1]:=
Click for copyable input
The first time the package is used you will need to run the RestoreGraphDatabase command. If you find that the examples in this section do not work as shown, you should also run this command. This command can take a long time to run, but is only necessary once.
Click for copyable input
The properties of the graphs stored in the database are given by the GraphProperties function.
In[2]:=
Click for copyable input
Out[2]=
Now, you can make a query from the database. This is done with the GraphQuery command. The following returns all complete graphs.
The format used for the graphs is that provided by the Combinatorica package. This draws a picture of the graph and also returns a symbolic object that could be used for further computation by Mathematica.
The following returns the first three regular graphs.
The following finds Hamiltonian graphs, returning their diameter, girth, and edge information. As before, a picture of the graph is also drawn.
One important aspect of this example package is that it shows commands that are specific to the issue of finding graphs rather than general database commands. The details of the database interactions are all placed in the implementation.

The Graph Database Package

The graph database package provides three functions.
GraphProperties[]list all graph properties within the database
GraphQuery[{props}]search the database for graphs that match props
RestoreGraphDatabase[]restore the data in the database (can be slow)

Graph database package functions.

The RestoreGraphDatabase function recomputes all the data in the database and can take some time to complete. Generally you do not want to run this, unless you have corrupted the database in some way.