commit - Make changes to MySQL database permanent - MATLAB (original) (raw)

Main Content

Make changes to MySQL database permanent

Since R2020b

Syntax

Description

commit([conn](#mw%5F268172b5-36b0-4384-a190-5f5bbbd9d6d4%5Fsep%5Fmw%5Fe8a1bdf2-6db9-421c-be5b-85d2f5c29cb1)) makes changes to the database connection conn permanent, specifically any changes made since the lastcommit or rollback function has been run. To use the commit function, you must set the AutoCommit property of the connection object to off.

example

Examples

collapse all

Commit Data to MySQL Database

Use a MySQL® native interface database connection to insert product data from MATLAB® into a new table in a MySQL database. Then, commit the changes to the database.

Create a MySQL native interface database connection to a MySQL database using the data source name, user name, and password. The database contains the tables productTable and suppliers.

datasource = "MySQLNative"; username = "root"; password = "matlab"; conn = mysql(datasource,username,password);

Allow manual committing of changes to the database by setting the AutoCommit property to off.

Create a MATLAB table that contains data for two products. The data is stored in the productTable and suppliers tables.

data = table([30;40],[500000;600000],[1000;2000],[25;30], ... ["Rubik's Cube";"Doll House"],'VariableNames',["productNumber" ... "stockNumber" "supplierNumber" "unitCost" "productDescription"]);

Insert the product data into a new table named toyTable.

tablename = "toyTable"; sqlwrite(conn,tablename,data)

Import the contents of the database table into MATLAB and display the rows. The results contain two rows for the inserted products.

rows = sqlread(conn,tablename)

rows=2×5 table productNumber stockNumber supplierNumber unitCost productDescription _____________ ___________ ______________ ________ __________________

     30             5e+05            1000            25         "Rubik's Cube"  
     40             6e+05            2000            30         "Doll House"    

Commit the changes to the database.

Close the database connection.

Input Arguments

collapse all

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.

Version History

Introduced in R2020b