rollback
Undo changes to MySQL database
Syntax
Description
rollback( reverses changes made to a
database using functions such as conn)sqlwrite. The
rollback function reverses all changes made since the last
COMMIT or ROLLBACK operation. To use this function,
you must set the AutoCommit property of the connection
object to off.
Examples
Reverse Changes Made 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, reverse the changes made 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. If the database requires
authentication, the recommended practice is to store credentials in your MATLAB® vault using setSecret
instead of including them in your code. To connect to the database, specify the
datasource and retrieve your credentials using the getSecret
function.
Before R2024a:
setSecret and getSecret are not available. Specify
username and password using character vectors or strings.
datasource = "MySQLNative"; setSecret(“usernamemysql”); setSecret(“passwordmysql”); conn = mysql(datasource,getSecret("usernamemysql"),getSecret("passwordmysql"));
Allow manual committing of changes to the database by setting the
AutoCommit property to off.
conn.AutoCommit = "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"
Reverse the changes made to the database.
rollback(conn)
Import and display the contents of the database table again. The results are empty.
rows = sqlread(conn,tablename)
rows = 0×5 empty table
Close the database connection.
close(conn)
Input Arguments
Version History
Introduced in R2020b