database.fetch - Execute SQL statement to import data into MATLAB® workspace

Syntax

results = fetch(conn, sqlquery)
results = fetch(conn, sqlquery, RowInc)

Description

RowInc, manages speed and memory issues. It is a good practice to use RowInc when importing large amounts of data.

For more information on SQL statements, see exec.

Remarks

Examples

Example 1 — Import Data

  1. Import the country column from the customers table in the SampleDB database.

    conn= database('SampleDB','','');
    setdbprefs('DataReturnFormat','cellarray')
    results=fetch(conn, 'select country from customers')
    results = 
    
        'Germany'
        'Mexico'
        'Mexico'
        'UK'
        'Sweden'
    
        ...
    
        'Finland'
        'Brazil'
        'USA'
        'Finland'
        'Poland'
  2. View the size of the cell array into which the results were returned.

    size(results)ans =
    
        91     1

Example 2— Import Two Columns of Data and View Information

  1. Import the ProductName and Discontinued columns from the SampleDB database.

    conn = database('SampleDB', '', '');
    setdbprefs('DataReturnFormat','cellarray')
    results=fetch(conn, 'select ProductName, Discontinued from Products');
  2. View the size of the cell array into which the results were returned.

    size(results)
    ans =
    
        77   2
  3. To see the results for the first row of data, run:

    results(1,:)
    ans = 
        'Chai'    [0]
  4. View the data type of the second element in the first row of data.

    class(results{1,2})
    ans =
    logical

See Also

cursor.fetch, database, exec, fetch, logical,

Retrieving BINARY or OTHER Sun™ Java™ SQL Data Types

  


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