commit
Make changes to database permanent
Syntax
Description
commit( makes all operations performed on
a database since the last conn)COMMIT or ROLLBACK
permanent. This function works only when the connection is in transactional mode
(AutoCommit is disabled). Because AutoCommit is
on by default, set AutoCommit to
off before using this function. Use commit after
completing operations you want to permanently save.
conn can be any of the following connection objects:
MySQL®
PostgreSQL®
DuckDB™
SQLite
ODBC
JDBC
Examples
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.
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
30 500000 1000 25 "Rubik's Cube"
40 600000 2000 30 "Doll House"
Commit the changes to the database.
commit(conn)
Close the database connection.
close(conn)
Use a PostgreSQL native interface database connection to insert product data from MATLAB® into a new table in a PostgreSQL database. Then, commit the changes to the database.
Create a PostgreSQL native interface database connection to a PostgreSQL database using the data source name, user name, and password. The database contains the tables productTable and suppliers.
datasource = "PostgreSQLDataSource"; username = "dbdev"; password = "matlab"; conn = postgresql(datasource,username,password);
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.
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 database table 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
30 500000 1000 25 "Rubik's Cube"
40 600000 2000 30 "Doll House"
Commit the changes to the database.
commit(conn)
Close the database connection.
close(conn)
Create a transient, in-memory DuckDB™ database connection by using the duckdb function. Then, create a table by using the execute function.
conn = duckdb();
execute(conn,"CREATE TABLE demo (COL1 INTEGER)");To manually commit changes, you must set the AutoCommit property to "off".
conn.AutoCommit = "off";Next, use the execute function to insert data into the table. Then commit the changes to the database by using the commit function.
execute(conn,"INSERT INTO demo VALUES (1)");
commit(conn);Verify that the changes were made to the database table by using the fetch function to access the table and import the data into MATLAB®.
data = fetch(conn,"SELECT * FROM demo")data=table
COL1
____
1
Close the connection.
close(conn);
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.
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"
Commit the changes to the database.
commit(conn)
Delete the new table to maintain the dataset.
sqlquery = "DROP TABLE toyTable";
execute(conn,sqlquery)Close the database connection.
close(conn)
Connect to a MySQL database by using an ODBC database connection. Then insert some data and commit the changes to the database.
Create a database connection to the ODBC data source, MySQL ODBC.
Before R2024a: setSecret and
getSecret are not available. Specify username and password using
character vectors or strings.
DataSourceName = "MySQL ODBC"; setSecret("usernamemysql"); setSecret("passwordmysql"); conn = database(DataSourceName,getSecret("usernamemysql"),getSecret("passwordmysql"))
conn =
connection with properties:
DataSource: 'MySQL ODBC'
UserName: ''
Message: ''
Type: 'ODBC Connection Object'
Database Properties:
AutoCommit: 'on'
ReadOnly: 'off'
LoginTimeout: 0
MaxDatabaseConnections: 0
Catalog and Schema Information:
DefaultCatalog: 'toystore_doc'
Catalogs: {'information_schema', 'detsdb', 'mysql' ... and 4 more}
Schemas: {}
Database and Driver Information:
DatabaseProductName: 'MySQL'
DatabaseProductVersion: '5.7.22'
DriverName: 'myodbc5a.dll'
DriverVersion: '05.03.0014'
conn has an empty Message property, which indicates a successful connection.
The property sections of the conn object are:
Database Properties -- Information about the database configuration
Catalog and Schema Information -- Names of catalogs and schemas in the database
Database and Driver Information -- Names and versions of the database and driverAllow 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
30 500000 1000 25 "Rubik's Cube"
40 600000 2000 30 "Doll House"
Commit the changes to the database.
commit(conn)
Close the database connection.
close(conn)
Input Arguments
Database connection, specified as any of the following:
MySQL
connectionobject created by using themysqlfunction.PostgreSQL
connectionobject created by using thepostgresqlfunction.DuckDB
connectionobject created by using theduckdbfunction.SQLite
connectionobject created by using thesqlitefunction.ODBC
connectionobject created by using thedatabasefunction.JDBC
connectionobject created by using thedatabasefunction.
Version History
Introduced in R2020bYou can now use DuckDB
connection objects as inputs to the commit function to
make your database changes permanent.
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.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- 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)