Database Toolbox Previous page   Next Page

Creating and Running a Query to Import Data

Building and Executing a Query

Build and run a query to import data from your database into MATLAB. Then save the query for use again later.

Before You Start.   Before using the VQB, set up a data source--see Setting Up a Data Source. The examples here use the dbtoolboxdemo data source.

To Start.   To open the VQB, in the Command Window, type

In the VQB, perform these steps to create and run a query to retrieve data:

  1. In the Data operation field, choose Select, meaning you want to select data from a database.
  2. From the Data source list box, select the data source from which you want to import data. For this example, select dbtoolboxdemo, which is the data source for the tutorial database.
  1. After selecting a data source, the set of Tables in that data source appears.

  1. From the Tables list box, select the table that contains the data you want to import. For this example, select salesVolume. Table names that include spaces appear in quotation marks. For Microsoft Excel, the Tables are Excel sheets.
  1. After you select a table, the set of Fields (column names) in that table appears.

  1. From the Fields list box, select the fields containing the data you want to import. To select more than one field, hold down the Ctrl key or Shift key while selecting. For this example, select the fields StockNumber, January, February, and March. Field names that include spaces appear in quotation marks. To deselect an entry, use Ctrl+click.
  1. As you select items from the Fields list, the query appears in the SQL statement field.

  1. In the MATLAB workspace variable field, assign a name for the data returned by the query. For this example, use A.
  2. Click Execute to run the query and retrieve the data.
  1. The query runs, retrieves data, and stores it in MATLAB, which in this example is a cell array assigned to the variable A. In the Data area, information about the query result appears.

    If any of the data to be retrieved is a Java BINARY or OTHER type, for example, a bitmap image, the retrieval might be time intensive. For more information about retrieving this type of data, see Retrieving BINARY and OTHER Java Data.

    If an error dialog box appears, the query is invalid. For example, you cannot perform a query on table and field names that contain quotation marks.

  1. Double-click A in the Data area. The contents of A are displayed in the Array Editor, where you can view and edit the data. See the MATLAB Array Editor documentation for details about using it.

  1. Another way to see the contents of A is to type A in the Command Window.

    As an example of how to read the results, sales for item 400876 are 3000 in January, 2400 in February, and 1500 in March.

    Note that if the data contains the Java OTHER data type, some fields in A might be empty. This happens when Java cannot pass the data through the JDBC/ODBC bridge.


Previous page  Help and Demos for the Visual Query Builder Saving, Editing, Clearing Variables for, and Specifying Preferences for Queries Next page

© 1994-2005 The MathWorks, Inc.