agentleFS
yuchenmiao04yuchenmiao04/My_skills/skills/matlab/matlab-write-database/SKILL.md

matlab-write-database

Skill8 starsChanged 5 months agoMITView on GitHub

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.

The file

We split this skill into the rules it gives an agent

11 rules. About 0 are common, 2 are distinctive, and 9 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈2

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 9

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Use when writing data to relational databases, executing SQL statements, managing transactions, running stored procedures, or using SQL …

    line 12
  2. Very specific:

    ALWAYS: use setSecret / getSecret (R2024a+) for credential storage.

    line 37
  3. Very specific:

    ALWAYS: use rollback in error-handling blocks to undo partial changes on failure.

    line 46
  4. Very specific:

    Type mismatches: Ensure MATLAB column types match DB column types (e.g., int32 not double for INTEGER columns)

    line 79
  5. Very specific:

    ALWAYS: use sqlwrite for inserting MATLAB tables — it handles type mapping automatically.

    line 136
  6. Very specific:

    ALWAYS: prefer sqlupdate (R2023a+) over raw SQL UPDATE — it uses rowfilter for type-safe filtering.

    line 137
  7. Very specific:

    Prefer transactions (commit/rollback) for multi-statement operations requiring atomicity.

    line 139
  8. Distinctive:

    Use prepared statements for repeated parameterized operations — they improve performance and prevent SQL injection.

    line 141
  9. Very specific:

    Solution: Verify AutoCommit was set to 'off' before the transaction.

    line 200
  10. Very specific:

    Solution: Verify the parameter indices in bindParamValues match the number of ?

    line 203
  11. Very specific:

    Solution: Verify the input argument count matches the stored procedure definition.

    line 209

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

MATLAB Database Export Architect

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.

When to Use This Skill

  • Writing/inserting MATLAB data into a database table
  • Updating existing rows in a database table
  • Deleting data from a database
  • Executing arbitrary SQL statements (DDL, DML)
  • Running stored procedures or custom database functions
  • Managing transactions (commit/rollback)
  • Using SQL prepared statements for parameterized queries
  • Creating or altering database table structures
  • User mentions keywords: write, export, insert, sqlwrite, sqlupdate, update, delete, execute, stored procedure, runstoredprocedure, transaction, commit, rollback, DDL, CREATE TABLE, ALTER, prepared statement, bulk insert

When NOT to Use

  • Reading/importing data from a database — use sqlread/fetch with RowFilter and databaseImportOptions
  • Object-oriented writes with class mapping — use ORM (ormwrite/ormupdate with Mappable classes)
  • MongoDB, Cassandra, or Neo4j writes — these have their own document/graph-specific write interfaces
  • Large data that doesn't fit in memory — use DatabaseDatastore + chunked processing for reads, or chunked sqlwrite loops for writes

Critical Rules

Credential Security

  • NEVER hardcode passwords or credentials in generated code.
  • ALWAYS use setSecret / getSecret (R2024a+) for credential storage.

Database Connection

  • ALWAYS establish and verify a connection before any export operation. Check isopen(conn) after connecting.
  • ALWAYS close connections with close(conn) when done.

Transaction Safety

  • ALWAYS set AutoCommit to off when using commit / rollback for transaction control.
  • ALWAYS wrap multi-statement operations in a transaction when atomicity is required.
  • ALWAYS use rollback in error-handling blocks to undo partial changes on failure.

Decision Framework

GoalFunctionWhen to Use
Write MATLAB table to DB tablesqlwriteBulk insert of tabular data; creates table if needed
Modify existing rowssqlupdateUpdate specific rows matching a filter (R2023a+)
Run DDL or raw SQLexecuteCREATE TABLE, DROP, ALTER, simple CALL statements
Repeated parameterized insertsdatabasePreparedStatementHigh-frequency inserts with varying values
Stored procedure with typed outputsrunstoredprocedureNeed typed output arguments from stored procedure (JDBC/ODBC only)
Simple stored procedure callexecute with CALLNo 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 Reference

FunctionPurposeSince
sqlwriteInsert MATLAB table into database tableR2018a
sqlupdateUpdate rows in database table matching a filterR2023a
updateReplace data in database table (legacy)R2006a
executeExecute any SQL statement (DDL, DML, stored procs)R2018b
runstoredprocedureCall stored procedure with input/output arguments (JDBC/ODBC only)R2006b
commitMake database changes permanentR2006a
rollbackUndo database changesR2006a
databasePreparedStatementCreate SQL prepared statement (JDBC only)R2019b
bindParamValuesBind values to prepared statement parametersR2019b

Edge Cases

  • NULL values: MATLAB missing, NaN, or empty "" map to SQL NULL in sqlwrite
  • Type mismatches: Ensure MATLAB column types match DB column types (e.g., int32 not double for INTEGER columns)
  • Auto-increment PKs: Omit the auto-increment column from the MATLAB table before calling sqlwrite

Core Concepts

See knowledge cards for detailed usage and examples:

  • Insert and update data: reference/cards/sqlwrite-sqlupdate.md
  • Execute SQL and stored procedures: reference/cards/execute-storedproc.md
  • Transaction management: reference/cards/transactions.md
  • Prepared statements (JDBC only): reference/cards/prepared-statements.md

Complete Examples

See knowledge cards for complete examples:

  • Insert computed results: reference/cards/sqlwrite-sqlupdate.md
  • Atomic multi-table update with transaction: reference/cards/transactions.md
  • Parameterized insert with prepared statement: reference/cards/prepared-statements.md

Common Mistakes

% 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);

Best Practices

  • ALWAYS use sqlwrite for inserting MATLAB tables — it handles type mapping automatically.
  • ALWAYS prefer sqlupdate (R2023a+) over raw SQL UPDATE — it uses rowfilter for type-safe filtering.
  • ALWAYS close connections with close(conn) when done.
  • Prefer transactions (commit/rollback) for multi-statement operations requiring atomicity.
  • For very large inserts, chunk the MATLAB table manually in a loop to avoid memory issues.
  • Use prepared statements for repeated parameterized operations — they improve performance and prevent SQL injection.
  • Prepared statements are JDBC only — not available for ODBC or native connections.
  • runstoredprocedure is JDBC/ODBC only (database() connections) — not available for native connections (sqlite, postgresql, mysql, duckdb). Use execute with a CALL statement instead.

Common Patterns

Pattern 1: Insert-Verify

sqlwrite(conn, "myTable", data);
result = sqlread(conn, "myTable");
disp("Rows after insert: " + height(result));

Pattern 2: Transaction-Protected Update

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';

Pattern 3: DDL-then-Insert

execute(conn, "CREATE TABLE IF NOT EXISTS results (ID INT, Value DOUBLE)");
sqlwrite(conn, "results", data);

Checklist

Before finalizing, verify:

  • Database connection established and verified (isopen(conn))
  • No hardcoded credentials — uses getSecret or placeholders
  • sqlwrite used for table inserts (not raw SQL INSERT for MATLAB data)
  • Transactions used for multi-statement atomic operations
  • AutoCommit restored to 'on' after transaction blocks
  • Prepared statements closed with close(pstmt)
  • Connection closed with close(conn) at the end

Troubleshooting

Issue: sqlwrite fails with "table already exists"

  • Solution: 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

  • Solution: sqlupdate requires R2023a or later. For older releases, use update or execute a raw SQL UPDATE statement with execute.

Issue: sqlupdate silently updates wrong number of rows

  • Solution: The data table passed to sqlupdate must have either exactly 1 row (broadcasts to all matching rows) or exactly as many rows as the filter matches. ALWAYS verify the filter match count first with sqlread + the same RowFilter.

Issue: Transaction changes not visible after commit

  • Solution: Verify 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"

  • Solution: Verify the parameter indices in bindParamValues match the number of ? placeholders in the SQL statement. Indices are 1-based.

Issue: Bulk insert runs out of memory

  • Solution: 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"

  • Solution: Verify the input argument count matches the stored procedure definition. For JDBC connections, output types must use java.sql.Types constants.

Copyright 2026 The MathWorks, Inc.


Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.