execute
Execute SQL statement using MySQL native interface database connection
Since R2020b
Syntax
Description
Examples
Execute Non-SELECT
SQL Statement
Using a relational database connection, create and execute a non-SELECT
SQL statement that deletes a database table. The MySQLNative
data source configures a database connection to a MySQL® database.
This example uses a MySQL database version 5.7.22 database and the MySQL Connector/C++ driver version 8.0.15.
Connect to the database using the data source name, user name, and password.
datasource = "MySQLNative"; username = "root"; password = "matlab"; conn = mysql(datasource,username,password);
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)
Import the data from the patients
database table.
data = sqlread(conn,tablename);
Delete the patients
database table using the execute
function.
sqlquery = strcat("DROP TABLE ",tablename);
execute(conn,sqlquery)
Ensure that the table no longer exists.
data = sqlfind(conn,tablename)
data = 0×5 empty table
Close the database connection.
close(conn)
Input Arguments
conn
— MySQL® native interface database connection
connection
object
MySQL native interface database connection, specified as a connection
object. Starting in R2024a, it is recommended that you use setSecret
and getSecret
to store and retrieve your credentials for databases that require authentication. For
more details, refer to this
example.
sqlquery
— SQL statement
character vector | string scalar
SQL statement, specified as a character vector or string scalar. The SQL statement
can be any valid non-SELECT
SQL statement.
The SQL statement can be a stored procedure that does not return any result sets.
For stored procedures that return one or more result sets, use the fetch
function.
For information about the SQL query language, see the SQL Tutorial.
Example: "DROP TABLE patients"
Data Types: char
| string
Version History
Introduced in R2020b
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.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- 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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)