querydb
Purpose
Executes a query on a database defined by connection string.
Synopsis
out = querydb(connstr,sqlstr,options);
Description
This function is unsupported and is meant as a
"simple" database connection tool. For more sophisticated connection
tools and full support please see the Matlab Database Toolbox.
JDBC connections require that the jdbc driver “.jar” file be
added to the Matlab java classpath. See the documentation for the Matlab
commands ‘javaaddpath’ and ‘javaclasspath’ for more information. For example,
using the MySQL Connector/J 3.1 driver you'll need to add the "mysql-connector-java-3.1.12-bin.jar"
file to your java class path.
Inputs
connstr : A connection string or a
structure created using builddbstr. See BUILDDBSTR for more information.
sqlstr : A SQL statement to be
executed on the connection. The SQL statement must be of proper syntax or it
will fail. Default behavior is geared toward SELECT statements that return
values. If attempting to execute a SQL command that doesn't return a value
(e.g., CREATE TABLE) set the 'rtype' option to 'none'.
NOTE: Use a seperate
program like Microsoft Access to formulate the SQL statement. Access queries
can require some small changes in syntax.
Options
rtype : [{'dso'} | 'cell' | 'none'] Return type,
default is return SQL recordset as a DataSet Object using parsemixed.m to parse
data in. If 'cell' then a cell array is returned with all values. If 'none'
then jdbc will execute an "update" type query and ODBC will issue
command as stated but will not retrieve rows, both will cause querydb to return
empty.
varlabels
: [ {'none'} | 'fieldnames' ] Defines what should be used as variable
labels on output DataSet Object (only used when rtype is 'dso'). 'fieldnames'
uses the SQL field names for variable labels.
conntype
: [ 'jdbc' | {'odbc'} ] Determines type of connection. ODBC uses a
Windows ADO with Matlab (descibed above).
Examples
Assuming there is a connection string named ‘mydbconn’ already
created using the builddbstr command.
>> sqlstr = ‘SELECT * FROM myTable’;
>> mydso = querydb(mydbconn,sqlstr);
To return a cell array:
>> opts = querydb(‘options’);
>> opts.rtype = ‘cell’;
>> mycell = querydb(mydbconn,sqlstr,opts);
See Also
builddbstr, parsemixed