Database Toolbox |
Syntax
Description
curs = fetch(curs, RowLimit)
imports rows of data from the open SQL cursor curs
(created using exec
), up to the maximum RowLimit
, into the object curs
. Data is stored in MATLAB in a cell array, structure, or numeric matrix, based on specifications made using setdbprefs
. It is common practice to assign the object returned by fetch
to the variable curs
from the open SQL cursor. The next time you run fetch
, records are imported starting with the row following RowLimit
. If you fetch large amounts of data that cause out of memory or speed problems, use RowLimit
to limit how much data is retrieved at once. You can use the Visual Query Builder GUI instead of functions to import data. See Visual Query Builder for details.
curs = fetch(curs)
imports rows of data from the open SQL cursor curs
, up to the RowLimit
specified by set
, into the object curs
. Data is stored in MATLAB in a cell array, structure, or numeric matrix, based on specifications you made using setdbprefs
. It is common practice to assign the object returned by fetch
to the variable curs
from the open SQL cursor. The next time you run fetch
, records are imported starting with the row following RowLimit
. If no RowLimit
was specified by set
, fetch
imports all remaining rows of data.
Running fetch
returns information about the cursor object, curs
, created using exec
. The Data
element of the cursor object contains the data returned by fetch
. The data types are preserved. After running fetch
, display the returned data by typing curs.Data
.
When a fetched field contains BOOLEAN data, it is represented as a logical
data type in MATLAB.
When a field in curs.Data
contains BINARY
or OTHER
data types, you might need to understand the content and process it before using it in MATLAB. See Retrieving BINARY or OTHER Java SQL Data Types for a specific example about processing bitmap image data using functions, and Retrieving BINARY and OTHER Java Data for a VQB example.
Use get
to view properties of curs
.
Example 1--Import All Rows of Data
Import all of the data into the cursor object 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]
The fetch
operation stores the data in a cell array contained in the cursor object field curs.Data
. To display data in curs.Data
, type
MATLAB returns all of the data, which in this example consists of 1 column and 91 rows, some of which are shown here.
Example 2--Import Specified Number of Rows of Data
Specify the RowLimit
argument to retrieve the first 3 rows of data.
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]
Entering the fetch
function again returns the second 3 rows of data. Adding the semicolon suppresses display of the results.
Example 3--Import Numeric Data
Import a column of data that is known to be numeric. Use setdbprefs
to specify the format for the retrieved data as numeric
.
conn = database('SampleDB', '', '');
curs=exec(conn, 'select all UnitsInStock from Products');
setdbprefs('DataReturnFormat','numeric')
curs=fetch(curs,3);
curs.Data
MATLAB retrieves the data into a numeric matrix.
Example 4--Import BOOLEAN Data
Import data that includes a BOOLEAN field. Use setdbprefs
to specify the format for the retrieved data as cellarray
.
conn = database('SampleDB', '', '');
curs=exec(conn, 'select ProductName,Discontinued from
Products');
setdbprefs('DataReturnFormat','cellarray')
curs=fetch(curs,5);
A=curs.Data
A =
'Chai' [0]
'Chang' [0]
'Aniseed Syrup' [0]
[1x28 char] [0]
[1x22 char] [1]
View the class of the second column in A
.
See Also
attr
, cols
, columnnames
, exec
, get
, logical
, rows
, resultset
, set
, width
Retrieving BINARY or OTHER Java SQL Data Types
exportedkeys | get |
© 1994-2005 The MathWorks, Inc.