cursor.fetch

Import data into MATLAB® workspace from cursor object created by exec

GUI Alternatives

Retrieve data using Visual Query Builder. For more information about Visual Query Builder, see Using Visual Query Builder.

Syntax

curs = fetch(curs, RowLimit)
curs = fetch(curs)

Description

Data is stored in a MATLAB cell array, structure, or numeric matrix. It is a best practice to assign the object returned by fetch to the variable curs from the open SQL cursor. This practice results in only one open cursor object, which consumes less memory than multiple open cursor objects.

The next time fetch is run, records are imported starting with the row following the specified RowLimit. If you do not specify a RowLimit, fetch imports all remaining rows of data.

Fetching large amounts of data can result in memory or speed issues. In this case, use RowLimit to limit how much data you retrieve at once.

Remarks

This page documents fetch for a cursor object. For more information about the use of fetch, cursor.fetch, and database.fetch, see fetch. Unless otherwise noted, fetch in this documentation refers to cursor.fetch, rather than database.fetch.

Examples

Example 1: Import All Rows of Data

  1. Use fetch to import all data into the cursor object curs, and store the data in a cell array contained in the cursor object field curs.Data.

    .

    curs = fetch(curs)
    curs =
        Attributes: []
              Data: {91x1 cell}
    DatabaseObject: [1x1 database]
          RowLimit: 0
          SQLQuery: 'select country from customers'
           Message: []
              Type: 'Database Cursor Object'
         ResultSet: [1x1 sun.jdbc.odbc.JdbcOdbcResultSet]
            Cursor: [1x1 com.mathworks.toolbox.database.sqlExec]
         Statement: [1x1 sun.jdbc.odbc.JdbcOdbcStatement]
             Fetch: [1x1 com.mathworks.toolbox.database.fetchTheData]
    
  2. Display data in curs.Data. Due to space constraints, only a portion of the returned data appears here.

    curs.Data
    ans = 
        'Germany'
        'Mexico'
        'Mexico'
        'UK'
        'Sweden'
        	.
        	.
        	.
        'USA'
        'Finland'
        'Poland'
    

Example 2 — Import a Specified Number of Rows

    1. Use the RowLimit argument to retrieve only the first three rows of data.

      curs = fetch(curs, 3)
      curs =
              Attributes: []
                    Data: {3x1 cell}
          DatabaseObject: [1x1 database]
                RowLimit: 0
                SQLQuery: 'select country from customers'
                 Message: []
                    Type: 'Database Cursor Object'
               ResultSet: [1x1 sun.jdbc.odbc.JdbcOdbcResultSet]
                  Cursor: ...
      					[1x1 com.mathworks.toolbox.database.sqlExec]
               Statement: [1x1 sun.jdbc.odbc.JdbcOdbcStatement]
                   Fetch: ...
      					[1x1  com.mathworks.toolbox.database.fetchTheData]
      
    2. View the data.

      curs.Data
      ans = 
          'Germany'
          'Mexico'
          'Mexico'
      
    1. Rerun the fetch function to return the second three rows of data.

      curs = fetch(curs, 3);
      
    2. View the data.

      curs.Data
      ans = 
          'UK'
          'Sweden'
          'Germany'
      

Example 3 — Import Rows Iteratively until You Retrieve All Data

Use the RowLimit argument to retrieve the first ten rows of data, and then rerun the import using a while loop, retrieving ten rows at a time. Continue until you have retrieved all data, which occurs when curs.Data is 'No Data'.

% Initialize RowLimit (fetchsize)
fetchsize = 10
% Check for more data. Retrieve and display all data.
while ~strcmp(curs.Data, 'No Data')
	curs=fetch(curs,fetchsize);
	curs.Data(:)
end
ans = 
    'No Data'

Example 4 — Import Numeric Data

Import a column of numeric data, using the setdbprefs function to specifynumeric as the format for the retrieved data.

conn = database('SampleDB', '', '');
curs=exec(conn, 'select all UnitsInStock from Products');
setdbprefs('DataReturnFormat','numeric')
curs=fetch(curs,3);
curs.Data
ans = 
    39
    17
    13

Example 5 — Import BOOLEAN Data

  1. Import data that includes a BOOLEAN field, using the setdbprefs function to specify cellarray as the format for the retrieved data.

    conn = database('SampleDB', '', '');
    curs=exec(conn, 'select ProductName, ... 
    Discontinued fromProducts');
    setdbprefs('DataReturnFormat','cellarray')
    curs=fetch(curs,5);
    A=curs.Data
    A = 
        'Chai'             [0]
        'Chang'            [0]
        'Aniseed Syrup'    [0]
            [1x28 char]    [0]
            [1x22 char]    [1]
    
  2. View the class of the second column of A:

    class(A{1,2}
    ans =
    logical
    

See Also

attr, cols, columnnames, database, database.fetch, exec, fetch, fetchmulti, get, logical, rows, resultset, set, width, Using Visual Query Builder,

Retrieving BINARY or OTHER Sun™ Java™ SQL Data Types

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS