getoptions
Retrieve import options for database data
Description
returns the import options for all variables in the varOpts
= getoptions(opts
)SQLImportOptions
object.
Examples
Control the import options by creating an SQLImportOptions
object. Then, retrieve the default import options from a database table.
This example uses the patients.xls
spreadsheet, which contains patient information. Also, the example uses a Microsoft® SQL Server® Version 11.00.2100 database and the Microsoft SQL Server Driver 11.00.5058.
Create a database connection to a Microsoft SQL Server database with Windows® authentication. Specify a blank user name and password.
datasource = 'MS SQL Server Auth'; conn = database(datasource,'','');
Load patient information into the MATLAB® workspace.
patients = readtable('patients.xls');
Create the patients
database table using the patient information.
tablename = 'patients';
sqlwrite(conn,tablename,patients)
Create an SQLImportOptions
object using the patients
database table and the databaseImportOptions
function.
opts = databaseImportOptions(conn,tablename);
Retrieve and display the default import options for the patients
database table.
varOpts = getoptions(opts)
varOpts = 1x10 SQLVariableImportOptions array with properties: Variable Options: (1) | (2) | (3) | (4) | (5) | (6) | (7) | (8) | (9) | (10) Name: 'LastName' | 'Gender' | 'Age' | 'Location' | 'Height' | 'Weight' | 'Smoker' | 'Systolic' | 'Diastolic' | 'SelfAssessedHealthStatus' Type: 'char' | 'char' | 'double' | 'char' | 'double' | 'double' | 'double' | 'double' | 'double' | 'char' FillValue: '' | '' | [NaN] | '' | [NaN] | [NaN] | [NaN] | [NaN] | [NaN] | '' To access sub-properties of each variable, use getoptions
To modify the variable import options, see the setoptions
function.
Delete the patients
database table using the execute
function.
sqlquery = ['DROP TABLE ' tablename];
execute(conn,sqlquery)
Close the database connection.
close(conn)
Control the import options by creating an SQLImportOptions
object. Then, retrieve the default import options for several columns from a database table. Specify the columns to retrieve by using the database column names.
This example uses the patients.xls
spreadsheet, which contains the columns LastName
, Age
, and Location
. Also, the example uses a Microsoft® SQL Server® Version 11.00.2100 database and the Microsoft SQL Server Driver 11.00.5058.
Create a database connection to a Microsoft SQL Server database with Windows® authentication. Specify a blank user name and password.
datasource = 'MS SQL Server Auth'; conn = database(datasource,'','');
Load patient information into the MATLAB® workspace.
patients = readtable('patients.xls');
Create the patients
database table using the patient information.
tablename = 'patients';
sqlwrite(conn,tablename,patients)
Create an SQLImportOptions
object using the patients
database table and the databaseImportOptions
function.
opts = databaseImportOptions(conn,tablename);
Specify the names of the database columns in the patients
database table.
varnames = {'LastName','Age','Location'};
Retrieve and display the default import options for the specified database columns.
varOpts = getoptions(opts,varnames)
varOpts = 1x3 SQLVariableImportOptions array with properties: Variable Options: (1) | (2) | (3) Name: 'LastName' | 'Age' | 'Location' Type: 'char' | 'double' | 'char' FillValue: '' | [NaN] | '' To access sub-properties of each variable, use getoptions
To modify the variable import options, see the setoptions
function.
Delete the patients
database table using the execute
function.
sqlquery = ['DROP TABLE ' tablename];
execute(conn,sqlquery)
Close the database connection.
close(conn)
Control the import options by creating an SQLImportOptions
object. Then, retrieve the default import options for several columns from a database table. Specify the columns to retrieve by using a numeric index.
This example uses the patients.xls
spreadsheet, which contains the columns LastName
, Gender
, and Age
. Also, the example uses a Microsoft® SQL Server® Version 11.00.2100 database and the Microsoft SQL Server Driver 11.00.5058.
Create a database connection to a Microsoft SQL Server database with Windows® authentication. Specify a blank user name and password.
datasource = 'MS SQL Server Auth'; conn = database(datasource,'','');
Load patient information into the MATLAB® workspace.
patients = readtable('patients.xls');
Create the patients
database table using the patient information.
tablename = 'patients';
sqlwrite(conn,tablename,patients)
Create an SQLImportOptions
object using the patients
database table and the databaseImportOptions
function.
opts = databaseImportOptions(conn,tablename);
Specify the first three database columns by using a numeric index.
index = [1,2,3];
Retrieve and display the default import options for the specified database columns.
varOpts = getoptions(opts,index)
varOpts = 1x3 SQLVariableImportOptions array with properties: Variable Options: (1) | (2) | (3) Name: 'LastName' | 'Gender' | 'Age' Type: 'char' | 'char' | 'double' FillValue: '' | '' | [NaN] To access sub-properties of each variable, use getoptions
To modify the variable import options, see the setoptions
function.
Delete the patients
database table using the execute
function.
sqlquery = ['DROP TABLE ' tablename];
execute(conn,sqlquery)
Close the database connection.
close(conn)
Input Arguments
Database import options, specified as an SQLImportOptions
object.
Variable names, specified as a character vector, cell array of character vectors, string
scalar, string array, or numeric vector. The
varnames
input argument indicates
the variables in the VariableNames
property
of the SQLImportOptions
object to use for
importing data.
Example: 'productname'
Data Types: double
| char
| string
| cell
Index, specified as a numeric vector that identifies the variables in the
VariableNames
property of the SQLImportOptions
object to use for importing data.
Example: [1,2,3]
Data Types: double
Output Arguments
Type-dependent options for selected variables, returned as an array of variable
import options objects. The array contains an object corresponding to each variable in
the opts
input argument or in the selected variables specified by
the varnames
or index
input argument. The data
type of each object in the array depends on the data type of the corresponding
variable.
For categorical
and datetime
data types, each
variable import options object contains additional properties that correspond to the
data type.
To modify the properties of the individual objects, use the setoptions
function.
Version History
Introduced in R2018b
See Also
databaseImportOptions
| setoptions
| reset
| close
| database
| execute
| sqlwrite
| sqlread
External Websites
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Sélectionner un site web
Choisissez un site web pour accéder au contenu traduit dans votre langue (lorsqu'il est disponible) et voir les événements et les offres locales. D’après votre position, nous vous recommandons de sélectionner la région suivante : .
Vous pouvez également sélectionner un site web dans la liste suivante :
Comment optimiser les performances du site
Pour optimiser les performances du site, sélectionnez la région Chine (en chinois ou en anglais). Les sites de MathWorks pour les autres pays ne sont pas optimisés pour les visites provenant de votre région.
Amériques
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)