This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

read

Read data in DatabaseDatastore

Syntax

data = read(dbds)
[data,info] = read(dbds)

Description

example

data = read(dbds) returns data from the DatabaseDatastore object in increments specified by the ReadSize property of the DatabaseDatastore object. Subsequent calls to the read function continue reading from the endpoint of the previous call.

Note

read returns data as a table only. read ignores database preference settings for data return formatting.

If there is no more data to read from the query, read throws an error.

example

[data,info] = read(dbds) returns database information info.

Note

The syntax data = read(dbds,rowcount) has been removed. Set the DatabaseDatastore property ReadSize instead.

Examples

collapse all

Using a JDBC driver, create a database connection to a Microsoft® SQL Server® database with Windows® authentication. Specify a blank user name and password. The code assumes that you are connecting to database toy_store, database server dbtb04, and port number 54317.

conn = database('toy_store','','','Vendor','Microsoft SQL Server', ...
    'Server','dbtb04','PortNumber',54317,'AuthType','Windows');

Create a DatabaseDatastore object using the database connection and SQL query. This SQL query retrieves all data from the table. Specify reading a maximum of 10 records from the executed SQL query.

sqlquery = 'select * from airlinesmall';

dbds = databaseDatastore(conn,sqlquery,'ReadSize',10);

Read the data in the DatabaseDatastore object.

data = read(dbds)
data = 

    Year    Month    DayofMonth    DayOfWeek    DepTime    CRSDepTime    ArrTime    CRSArrTime    UniqueCarrier    FlightNum    TailNum    ActualElapsedTime    CRSElapsedTime    AirTime    ArrDelay    DepDelay    Origin    Dest     Distance    TaxiIn    TaxiOut    Cancelled    CancellationCode    Diverted    CarrierDelay    WeatherDelay    NASDelay    SecurityDelay    LateAircraftDelay
    ____    _____    __________    _________    _______    __________    _______    __________    _____________    _________    _______    _________________    ______________    _______    ________    ________    ______    _____    ________    ______    _______    _________    ________________    ________    ____________    ____________    ________    _____________    _________________

    1987    10       28            3            1140       1140          1212       1215          'US'             262          'NA'       32                    35               'NA'        -3          0          'CLE'     'PIT'    105         'NA'      'NA'       0            'NA'                0           'NA'            'NA'            'NA'        'NA'             'NA'             
    1987    10        9            5            1155       1155          1250       1300          'US'             282          'NA'       55                    65               'NA'       -10          0          'ROC'     'LGA'    254         'NA'      'NA'       0            'NA'                0           'NA'            'NA'            'NA'        'NA'             'NA'             
    1987    10       22            4             715        715           807        803          'US'             304          'NA'       52                    48               'NA'         4          0          'DTW'     'PIT'    201         'NA'      'NA'       0            'NA'                0           'NA'            'NA'            'NA'        'NA'             'NA'             
    1987    10       16            5            1553       1555          1641       1640          'US'             323          'NA'       48                    45               'NA'         1         -2          'BUF'     'PIT'    186         'NA'      'NA'       0            'NA'                0           'NA'            'NA'            'NA'        'NA'             'NA'             
    1987    10       30            5            1821       1815          1956       1955          'US'             346          'NA'       95                   100               'NA'         1          6          'IND'     'EWR'    644         'NA'      'NA'       0            'NA'                0           'NA'            'NA'            'NA'        'NA'             'NA'             
    1987    10       12            1            1300       1300          1529       1528          'US'             370          'NA'       89                    88               'NA'         1          0          'STL'     'PIT'    553         'NA'      'NA'       0            'NA'                0           'NA'            'NA'            'NA'        'NA'             'NA'             
    1987    10        7            3             810        810           904        900          'US'             387          'NA'       54                    50               'NA'         4          0          'ELM'     'PIT'    208         'NA'      'NA'       0            'NA'                0           'NA'            'NA'            'NA'        'NA'             'NA'             
    1987    10       19            1             733        735           827        831          'US'             407          'NA'       54                    56               'NA'        -4         -2          'ROC'     'CLE'    245         'NA'      'NA'       0            'NA'                0           'NA'            'NA'            'NA'        'NA'             'NA'             
    1987    10       15            4             828        830           916        921          'US'             424          'NA'       48                    51               'NA'        -5         -2          'ORF'     'PHL'    211         'NA'      'NA'       0            'NA'                0           'NA'            'NA'            'NA'        'NA'             'NA'             
    1987    10        4            7            1750       1735          1837       1816          'US'             449          'NA'       47                    41               'NA'        21         15          'PIT'     'CLE'    105         'NA'      'NA'       0            'NA'                0           'NA'            'NA'            'NA'        'NA'             'NA'             

data contains the query results.

Close the DatabaseDatastore object and database connection.

close(dbds)

Using a JDBC driver, create a database connection to a Microsoft® SQL Server® database with Windows® authentication. Specify a blank user name and password. The code assumes that you are connecting to database toy_store, database server dbtb04, and port number 54317.

conn = database('toy_store','','','Vendor','Microsoft SQL Server', ...
    'Server','dbtb04','PortNumber',54317,'AuthType','Windows');

Create a DatabaseDatastore object using the database connection and SQL query. This SQL query retrieves all data from the table. Specify reading a maximum of 10 records from the executed SQL query.

sqlquery = 'select * from airlinesmall';

dbds = databaseDatastore(conn,sqlquery,'ReadSize',10);

Read the data in the DatabaseDatastore object and retrieve information about the database.

[data,info] = read(dbds)
data = 

    Year    Month    DayofMonth    DayOfWeek    DepTime    CRSDepTime    ArrTime    CRSArrTime    UniqueCarrier    FlightNum    TailNum    ActualElapsedTime    CRSElapsedTime    AirTime    ArrDelay    DepDelay    Origin    Dest     Distance    TaxiIn    TaxiOut    Cancelled    CancellationCode    Diverted    CarrierDelay    WeatherDelay    NASDelay    SecurityDelay    LateAircraftDelay
    ____    _____    __________    _________    _______    __________    _______    __________    _____________    _________    _______    _________________    ______________    _______    ________    ________    ______    _____    ________    ______    _______    _________    ________________    ________    ____________    ____________    ________    _____________    _________________

    1987    10       28            3            1140       1140          1212       1215          'US'             262          'NA'       32                    35               'NA'        -3          0          'CLE'     'PIT'    105         'NA'      'NA'       0            'NA'                0           'NA'            'NA'            'NA'        'NA'             'NA'             
    1987    10        9            5            1155       1155          1250       1300          'US'             282          'NA'       55                    65               'NA'       -10          0          'ROC'     'LGA'    254         'NA'      'NA'       0            'NA'                0           'NA'            'NA'            'NA'        'NA'             'NA'             
    1987    10       22            4             715        715           807        803          'US'             304          'NA'       52                    48               'NA'         4          0          'DTW'     'PIT'    201         'NA'      'NA'       0            'NA'                0           'NA'            'NA'            'NA'        'NA'             'NA'             
    1987    10       16            5            1553       1555          1641       1640          'US'             323          'NA'       48                    45               'NA'         1         -2          'BUF'     'PIT'    186         'NA'      'NA'       0            'NA'                0           'NA'            'NA'            'NA'        'NA'             'NA'             
    1987    10       30            5            1821       1815          1956       1955          'US'             346          'NA'       95                   100               'NA'         1          6          'IND'     'EWR'    644         'NA'      'NA'       0            'NA'                0           'NA'            'NA'            'NA'        'NA'             'NA'             
    1987    10       12            1            1300       1300          1529       1528          'US'             370          'NA'       89                    88               'NA'         1          0          'STL'     'PIT'    553         'NA'      'NA'       0            'NA'                0           'NA'            'NA'            'NA'        'NA'             'NA'             
    1987    10        7            3             810        810           904        900          'US'             387          'NA'       54                    50               'NA'         4          0          'ELM'     'PIT'    208         'NA'      'NA'       0            'NA'                0           'NA'            'NA'            'NA'        'NA'             'NA'             
    1987    10       19            1             733        735           827        831          'US'             407          'NA'       54                    56               'NA'        -4         -2          'ROC'     'CLE'    245         'NA'      'NA'       0            'NA'                0           'NA'            'NA'            'NA'        'NA'             'NA'             
    1987    10       15            4             828        830           916        921          'US'             424          'NA'       48                    51               'NA'        -5         -2          'ORF'     'PHL'    211         'NA'      'NA'       0            'NA'                0           'NA'            'NA'            'NA'        'NA'             'NA'             
    1987    10        4            7            1750       1735          1837       1816          'US'             449          'NA'       47                    41               'NA'        21         15          'PIT'     'CLE'    105         'NA'      'NA'       0            'NA'                0           'NA'            'NA'            'NA'        'NA'             'NA'             


info = 

  struct with fields:

    datasource: 'toy_store'
        offset: 10

data contains the query results. The structure info contains the data source name datasource and current cursor position offset.

Close the DatabaseDatastore object and database connection.

close(dbds)

Input Arguments

collapse all

Datastore containing data in a database, specified as a DatabaseDatastore object created using the databaseDatastore function.

Output Arguments

collapse all

Query results, returned as a table of the records in the data set. Executing the SQL statement specified in the Query property of the DatabaseDatastore object creates the data set. The ReadSize property of the DatabaseDatastore object specifies the number of rows in the table.

If there is no more data to read from the query, read throws an error.

Database information, returned as a structure with these fields.

Field

Description

datasource

Data source name for ODBC drivers or a database name for JDBC drivers

offset

Current cursor position in the returned data set

Introduced in R2014b