Main Content

read

Read data in DatabaseDatastore

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

Create a database connection using a JDBC driver. To create this connection, you must configure a JDBC data source. For more information, see the configureJDBCDataSource function. Then, create a DatabaseDatastore object and read the data stored in the object.

Create a database connection to the JDBC data source MSSQLServerJDBCAuth. This data source configures a JDBC driver to a Microsoft® SQL Server® database with Windows® authentication. Specify a blank user name and password.

datasource = "MSSQLServerJDBCAuth";
username = "";
password = "";
conn = database(datasource,username,password);

Create a DatabaseDatastore object using the database connection and an SQL query. This SQL query retrieves all data from the airlinesmall 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 =

  10×29 table

    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          30            5         1329         1329        1434         1436           'US'            683        'NA'              65                 67           'NA'         -2           0       'LGA'     'BUF'       292       'NA'      'NA'          0              'NA'             0            'NA'            'NA'          'NA'          'NA'               'NA'       
    1987     11           7            6         1316         1315        1713         1647           'TW'            810        'NA'             177                152           'NA'         26           1       'STL'     'BOS'      1046       'NA'      'NA'          0              'NA'             0            'NA'            'NA'          'NA'          'NA'               'NA'       
    1987     11          28            6          815          815        1015         1015           'WN'            441        'NA'             120                120           'NA'          0           0       'BNA'     'HOU'       670       'NA'      'NA'          0              'NA'             0            'NA'            'NA'          'NA'          'NA'               'NA'       
    1987     11           2            1          700          700         800          800           'NW'            790        'NA'              60                 60           'NA'          0           0       'BTR'     'MEM'       319       'NA'      'NA'          0              'NA'             0            'NA'            'NA'          'NA'          'NA'               'NA'       
    1987     11          14            6          840          840        1127         1120           'CO'            733        'NA'             167                160           'NA'          7           0       'EWR'     'MCO'       938       'NA'      'NA'          0              'NA'             0            'NA'            'NA'          'NA'          'NA'               'NA'       
    1987     11           1            7         1625         1625        1823         1758           'DL'            957        'NA'             118                 93           'NA'         25           0       'EWR'     'CVG'       569       'NA'      'NA'          0              'NA'             0            'NA'            'NA'          'NA'          'NA'               'NA'       
    1987     11          26            4         1314         1315        1538         1542           'AA'            398        'NA'              84                 87           'NA'         -4          -1       'BNA'     'RDU'       443       'NA'      'NA'          0              'NA'             0            'NA'            'NA'          'NA'          'NA'               'NA'       
    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'       

data contains the query results.

Close the DatabaseDatastore object and the database connection.

close(dbds)

Create a database connection using a JDBC driver. To create this connection, you must configure a JDBC data source. For more information, see the configureJDBCDataSource function. Then, create a DatabaseDatastore object, read the data stored in the object, and retrieve information about the database.

Create a database connection to the JDBC data source MSSQLServerJDBCAuth. This data source configures a JDBC driver to a Microsoft® SQL Server® database with Windows® authentication. Specify a blank user name and password.

datasource = "MSSQLServerJDBCAuth";
username = "";
password = "";
conn = database(datasource,username,password);

Create a DatabaseDatastore object using the database connection and an SQL query. This SQL query retrieves all data from the airlinesmall 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 =

  10×29 table

    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          30            5         1329         1329        1434         1436           'US'            683        'NA'              65                 67           'NA'         -2           0       'LGA'     'BUF'       292       'NA'      'NA'          0              'NA'             0            'NA'            'NA'          'NA'          'NA'               'NA'       
    1987     11           7            6         1316         1315        1713         1647           'TW'            810        'NA'             177                152           'NA'         26           1       'STL'     'BOS'      1046       'NA'      'NA'          0              'NA'             0            'NA'            'NA'          'NA'          'NA'               'NA'       
    1987     11          28            6          815          815        1015         1015           'WN'            441        'NA'             120                120           'NA'          0           0       'BNA'     'HOU'       670       'NA'      'NA'          0              'NA'             0            'NA'            'NA'          'NA'          'NA'               'NA'       
    1987     11           2            1          700          700         800          800           'NW'            790        'NA'              60                 60           'NA'          0           0       'BTR'     'MEM'       319       'NA'      'NA'          0              'NA'             0            'NA'            'NA'          'NA'          'NA'               'NA'       
    1987     11          14            6          840          840        1127         1120           'CO'            733        'NA'             167                160           'NA'          7           0       'EWR'     'MCO'       938       'NA'      'NA'          0              'NA'             0            'NA'            'NA'          'NA'          'NA'               'NA'       
    1987     11           1            7         1625         1625        1823         1758           'DL'            957        'NA'             118                 93           'NA'         25           0       'EWR'     'CVG'       569       'NA'      'NA'          0              'NA'             0            'NA'            'NA'          'NA'          'NA'               'NA'       
    1987     11          26            4         1314         1315        1538         1542           'AA'            398        'NA'              84                 87           'NA'         -4          -1       'BNA'     'RDU'       443       'NA'      'NA'          0              'NA'             0            'NA'            'NA'          'NA'          'NA'               'NA'       
    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'       


info = 

  struct with fields:

    datasource: 'MSSQLServerJDBCAuth'
        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 the 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

Version History

Introduced in R2014b