matlab/matlab-write-database
Writes data from MATLAB to relational databases and performs database operations. Use when writing data with sqlwrite, updating rows with sqlupdate, executing SQL with execute, running stored procedures, managing transactions with commit/rollback, creating tables, or using SQL prepared statements.
npx skills add https://github.com/matlab/matlab-agentic-toolkit --skill matlab-write-database
Use when writing data to relational databases, executing SQL statements, managing transactions, running stored procedures, or using SQL prepared statements. Covers all Database Toolbox operations that modify the database.
sqlread/fetch with RowFilter and databaseImportOptionsormwrite/ormupdate with Mappable classes)DatabaseDatastore + chunked processing for reads, or chunked sqlwrite loops for writessetSecret / getSecret (R2024a+) for credential storage.isopen(conn) after connecting.close(conn) when done.AutoCommit to off when using commit / rollback for transaction control.rollback in error-handling blocks to undo partial changes on failure.DROP, TRUNCATE, DELETE, and ALTER statements that remove columns or modify constraints.execute() without first presenting the exact SQL to the user and receiving explicit approval.employees. All data will be permanently lost. Proceed?").| Goal | Function | When to Use |
|------|----------|-------------|
| Write MATLAB table to DB table | sqlwrite | Bulk insert of tabular data; creates table if needed |
| Modify existing rows | sqlupdate | Update specific rows matching a filter (R2023a+) |
| Run DDL or raw SQL | execute | CREATE TABLE, DROP, ALTER, simple CALL statements |
| Repeated parameterized inserts | databasePreparedStatement | High-frequency inserts with varying values |
| Stored procedure with typed outputs | runstoredprocedure | Need typed output arguments from stored procedure (JDBC/ODBC only) |
| Simple stored procedure call | execute with CALL | No typed output args needed; result sets returned |
> execute vs runstoredprocedure: Use runstoredprocedure when you need typed output arguments. Use execute with CALL for simple invocation or when you only need result sets.
| Function | Purpose | Since |
|----------|---------|-------|
| sqlwrite | Insert MATLAB table into database table | R2018a |
| sqlupdate | Update rows in database table matching a filter | R2023a |
| update | Replace data in database table (legacy) | R2006a |
| execute | Execute any SQL statement (DDL, DML, stored procs) | R2018b |
| runstoredprocedure | Call stored procedure with input/output arguments (JDBC/ODBC only) | R2006b |
| commit | Make database changes permanent | R2006a |
| rollback | Undo database changes | R2006a |
| databasePreparedStatement | Create SQL prepared statement (JDBC only) | R2019b |
| bindParamValues | Bind values to prepared statement parameters | R2019b |
missing, NaN, or empty "" map to SQL NULL in sqlwriteint32 not double for INTEGER columns)sqlwriteSee knowledge cards for detailed usage and examples:
reference/cards/sqlwrite-sqlupdate.mdreference/cards/execute-storedproc.mdreference/cards/transactions.mdreference/cards/prepared-statements.mdSee knowledge cards for complete examples:
reference/cards/sqlwrite-sqlupdate.mdreference/cards/transactions.mdreference/cards/prepared-statements.md% INCORRECT — inserting rows one at a time in a loop (very slow)
for i = 1:height(data)
sqlwrite(conn, "orders", data(i,:));
end
% CORRECT — batch insert the entire table at once
sqlwrite(conn, "orders", data);
% INCORRECT — no transaction control for multi-table writes
sqlwrite(conn, "orders", orderData);
sqlwrite(conn, "order_items", itemData); % if this fails, orders are orphaned
% CORRECT — use transaction control for atomic multi-table writes
conn.AutoCommit = 'off';
try
sqlwrite(conn, "orders", orderData);
sqlwrite(conn, "order_items", itemData);
commit(conn);
catch ME
rollback(conn);
conn.AutoCommit = 'on'; %#ok<NASGU> restore before rethrowing
rethrow(ME);
end
conn.AutoCommit = 'on';
% INCORRECT — including auto-increment column in sqlwrite
data = table(1, "Widget", 9.99, VariableNames=["ID", "Name", "Price"]);
sqlwrite(conn, "products", data); % Error if ID is auto-increment
% CORRECT — omit auto-increment column
data = table("Widget", 9.99, VariableNames=["Name", "Price"]);
sqlwrite(conn, "products", data);
% INCORRECT — single filter with multi-row data table
rf = rowfilter("Category");
filter = rf.Category == "Widgets"; % matches 2 rows
data = table([8.99; 12.99], VariableNames="Price"); % 2 rows
sqlupdate(conn, "products", data, filter); % Error: filters must match table height
% CORRECT — cell array of filters for multi-row update
rf = rowfilter("ProductID");
filters = {rf.ProductID == 1; rf.ProductID == 2};
data = table([8.99; 12.99], VariableNames="Price");
sqlupdate(conn, "products", data, filters);
sqlwrite for inserting MATLAB tables — it handles type mapping automatically.sqlupdate (R2023a+) over raw SQL UPDATE — it uses rowfilter for type-safe filtering.close(conn) when done.commit/rollback) for multi-statement operations requiring atomicity.runstoredprocedure is JDBC/ODBC only (database() connections) — not available for native connections (sqlite, postgresql, mysql, duckdb). Use execute with a CALL statement instead.DROP, TRUNCATE, DELETE, or column-dropping ALTER — present the SQL and wait for explicit approval, even if the user's request implied the operation.sqlwrite(conn, "myTable", data);
result = sqlread(conn, "myTable");
disp("Rows after insert: " + height(result));
conn.AutoCommit = 'off';
try
execute(conn, sqlStatement);
commit(conn);
catch e
rollback(conn);
conn.AutoCommit = 'on'; %#ok<NASGU> restore before rethrowing
rethrow(e);
end
conn.AutoCommit = 'on';
execute(conn, "CREATE TABLE IF NOT EXISTS results (ID INT, Value DOUBLE)");
sqlwrite(conn, "results", data);
Before finalizing, verify:
isopen(conn))getSecret or placeholderssqlwrite used for table inserts (not raw SQL INSERT for MATLAB data)AutoCommit restored to 'on' after transaction blocksclose(pstmt)close(conn) at the endDROP, TRUNCATE, DELETE, ALTER) confirmed with user before executionIssue: sqlwrite fails with "table already exists"
sqlwrite creates the table if it doesn't exist but errors if the table exists with a different schema. Use sqlwrite to append to an existing table — column names and types must match.Issue: sqlupdate not recognized
sqlupdate requires R2023a or later. For older releases, use update or execute a raw SQL UPDATE statement with execute.Issue: sqlupdate errors with "Number of filters must match the height of the table"
RowFilter only works with a 1-row data table (broadcasts to all matching rows). Do not pass a single filter that matches N rows with an N-row data table — this errors.Issue: Transaction changes not visible after commit
AutoCommit was set to 'off' before the transaction. If AutoCommit is 'on', each statement auto-commits immediately.Issue: Prepared statement errors with "parameter index out of range"
bindParamValues match the number of ? placeholders in the SQL statement. Indices are 1-based.Issue: Bulk insert runs out of memory
sqlwrite has no BatchSize parameter. Chunk the MATLAB table manually in a loop — split data into slices of 5,000–50,000 rows and call sqlwrite on each slice.Issue: runstoredprocedure fails with "wrong number of arguments"
java.sql.Types constants.----
Copyright 2026 The MathWorks, Inc.
----
Take matlab/matlab-write-database from the repository into ~/.claude/skills for personal
use, or into .claude/skills inside a project.
The agent identifies a skill by the name field in its header. Two skills with the
same name cannot sit side by side — one of them will be ignored.