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

Main Content

Make changes to SQLite database file permanent

Since R2022a

Syntax

Description

commit([conn](#mw%5F3a4db919-a6b1-42e6-a098-bf8bdf5aff87%5Fsep%5Fshared-conn)) makes changes to the SQLite database connection permanent using the MATLAB® interface to SQLite. This function makes permanent any changes made after the last commit or rollback function has been run. To use the commit function, you must set theAutoCommit property of the sqlite object to off.

example

Examples

collapse all

Commit Data to SQLite Database

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

Create the SQLite connection conn to the existing SQLite database file tutorial.db. The database file contains the table productTable. The SQLite connection is an sqlite object.

dbfile = "tutorial.db"; conn = sqlite(dbfile);

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.

Delete the new table to maintain the dataset.

sqlquery = "DROP TABLE toyTable"; execute(conn,sqlquery)

Close the database connection.

Input Arguments

collapse all

SQLite database connection, specified as an sqlite object created using the sqlite function.

Version History

Introduced in R2022a