Data Import Using Database Explorer App or Command Line
You can import data from a database into MATLAB® using the Database Explorer app or the command line. To select data for import, you can build an SQL query visually by using the Database Explorer app. Or, you can use the command line to write SQL queries. To achieve maximum performance with large data sets, use the command line instead of the Database Explorer app.
After importing data, you can repeat the steps in the process, such as connecting to a database, executing an SQL query, and so on, by using a MATLAB script to automate them.
To open multiple connections to the same database simultaneously, you can create multiple SQL queries using the Database Explorer app. Or, you can connect to the database using the command line.
If you do not have access to a database and want to import your data quickly, you can use the MATLAB interface to SQLite. For details, see Interact with Data in SQLite Database Using MATLAB Interface to SQLite.
Data Import Using Database Explorer App
If you have minimal proficiency writing SQL queries or want to browse the data in
a database quickly, use the Database Explorer app. To build queries,
see Create SQL Queries Using Database Explorer App. After
creating a query using the Database Explorer app, you can generate the SQL code for
the query. For details, see Generate SQL Query. You can
embed the generated SQL code into the SQL query that you specify in the fetch
function. Or, you can create
an SQL script file to use with the executeSQLScript
function.
If you want to automate the current task after you create the SQL query, then generate a MATLAB script. For details, see Generate MATLAB Script.
Data Import Using Command Line
If you are not familiar with writing SQL queries, then use the Database Explorer app to select data to
import from your database. Or, you can use the sqlread
function at the command line. This function needs only a
database connection and the database table name to import data. Furthermore, the
sqlread
function does not require you to set database
preferences.
If you know how to write SQL queries, you can write basic SQL statements as character vectors or string scalars. For a simple example, see Import Data from Database Table Using sqlread Function.
When writing SQL queries, you can import data into MATLAB in one of two ways. Use the select
function for maximum memory
efficiency and quick access to imported data. Or, use the fetch
function to import numeric
data with double precision by default or define the import strategy for the SQL
query.
For memory management, see Data Import Memory Management.
If you have a stored procedure that imports data, then use the runstoredprocedure
or fetch
functions.
Custom Data Types
When importing data from a database, Database Toolbox™ functions return custom data types, such as Oracle® ref cursors, as Java® objects. You can manually parse these objects to retrieve their data
contents. Use the methods
function to access all the
methods of a Java object. Use the available methods to retrieve data from a Java object. The steps for your object are specific to your database. For
details, refer to your JDBC driver or database documentation.
SQL Queries Saved in Scripts or Files
If you have a long SQL query or multiple SQL queries that you want to run
sequentially to import data, create an SQL script file containing your SQL queries.
To execute the SQL script file, use the executeSQLScript
function. If you have SQL queries stored in
.sql
or text files that you want to run from MATLAB, you can also use this function.
See Also
select
| fetch
| database
| sqlread