Skip to content

matlab/matlab-agentic-toolkit / skills-catalog/reporting-and-database-access/matlab-use-database

matlab-use-database

Verified

Reads from, writes to, and manages relational databases using MATLAB Database Toolbox. Use when connecting to databases, reading data with sqlread or fetch, filtering with rowfilter, writing with sqlwrite, updating with sqlupdate, executing SQL statements, managing transactions with commit and rollback, mapping MATLAB classes to tables with ORM (Mappable, ormread, ormwrite, ormupdate), or performing any database operation from MATLAB. Triggers on: database, SQL, sqlread, sqlwrite, sqlupdate, fetch, execute, rowfilter, RowFilter, ORM, Mappable, ormread, ormwrite, ormupdate, orm2sql, transaction, commit, rollback, Database Toolbox, PostgreSQL, MySQL, SQLite, SQL Server, Oracle, database connection, database table, query database, insert data, update rows, delete rows, stored procedure, prepared statement, odbc, databaseConnectionOptions, datasource, data source, DSN, connection string, multithreaded, parallel.

references/sqlwrite-sqlupdate.md

View on GitHub
# sqlwrite & sqlupdate — Insert and Update Data

## Insert Data with `sqlwrite`

```matlab
data = table(["Wrench"; "Hammer"], [50; 30], [4.99; 12.50], ...
    VariableNames=["Product", "Quantity", "Price"]);

sqlwrite(conn, "inventoryTable", data);
```

### `sqlwrite` Parameters

| Parameter | Description |
|-----------|-------------|
| `conn` | Database connection object |
| `tablename` | Target table name (string) |
| `data` | MATLAB table to insert |
| `Catalog` | Database catalog name |
| `Schema` | Database schema name |
| `ColumnType` | Cell array of SQL column types for new tables |

### Bulk Insert (Chunked)

`sqlwrite` has no `BatchSize` parameter. For large data, chunk manually:

```matlab
chunkSize = 50000;
numChunks = ceil(height(data) / chunkSize);
for c = 1:numChunks
    startIdx = (c - 1) * chunkSize + 1;
    endIdx = min(c * chunkSize, height(data));
    sqlwrite(conn, "largeTable", data(startIdx:endIdx, :));
    fprintf("Wrote chunk %d/%d\n", c, numChunks);
end
```

## Update Rows with `sqlupdate` (R2023a+)

```matlab
rf = rowfilter("Product");
data = table(5.99, VariableNames="Price");
sqlupdate(conn, "inventoryTable", data, rf.Product == "Wrench");
```

### Multi-Row Update (cell array of filters)

```matlab
rf = rowfilter("ProductID");
filters = {rf.ProductID == 1; rf.ProductID == 2};
data = table([5.99; 13.99], VariableNames="Price");
sqlupdate(conn, "inventoryTable", data, filters);
```

### `sqlupdate` Parameters

| Parameter | Description |
|-----------|-------------|
| `conn` | Database connection object |
| `tablename` | Target table name |
| `data` | MATLAB table with new values |
| `filter` | `RowFilter` object (positional 4th arg) or cell array of `RowFilter` objects |
| `Catalog` | Database catalog name |
| `Schema` | Database schema name |

## Complete Example: Read, Transform, Write Back

```matlab
conn = database("myDataSource", getSecret("dbUser"), getSecret("dbPass"));

sourceData = sqlread(conn, "salesData");
summary = groupsummary(sourceData, "Region", "mean", "Revenue");
sqlwrite(conn, "salesSummary", summary);

close(conn);
```

## Gotchas

- `sqlwrite` creates the table if it doesn't exist. When appending, column names and types must match.
- `sqlupdate` requires R2023a+. For older releases, use `execute` with raw SQL UPDATE.
- **`sqlupdate` filter is positional 4th arg** — NOT a name-value pair. `sqlupdate(conn, tbl, data, filter)`.
- A single `RowFilter` with a 1-row data table broadcasts to all matching rows. For multi-row updates with different values per row, pass a cell array of filters.
- `sqlwrite` has no `BatchSize` parameter — loop over chunks manually.
- **NEVER** call `sqlwrite` row-by-row in a loop — batch rows into a single table.

----

Copyright 2026 The MathWorks, Inc.

----
deprecation-and-migrationaddyosmani/agent-skillsManages deprecation and migration. Use when removing old systems, APIs, or features. Use when migrating users from one implementation to another. Use when migrating a database schema in production, such as renaming or dropping a column without downtime (expand/contract). Use when deciding whether to maintain or sunset existing code.Stars: 118kUpdated Claude CodeCodexVerifiedInstall
performance-optimizationaddyosmani/agent-skillsOptimizes application performance across frontend, backend, queries, and databases. Use when performance requirements exist, when you suspect performance regressions, when Core Web Vitals or load times need improvement, when N+1 query patterns need fixing, or when profiling reveals bottlenecks.Stars: 118kUpdated Claude CodeCodexVerifiedInstall
Agent Cost Reportthedotmack/claude-memBelievable agent cost report for any period, default the last 7 full days PT, not counting today. Measured tokens from Claude Code transcripts priced at OpenRouter list prices (ESTIMATED), measured provider spend when a sanctioned source exists, note-taker cost separate, Timing-style HTML/PDF plus report.json, line-items.csv, evidence.json.Stars: 117kUpdated Claude CodeCodexVerifiedInstall
Agent Cost Reportthedotmack/claude-memBelievable agent cost report for any period, default the last 7 full days PT, not counting today. Measured tokens from Claude Code transcripts priced at OpenRouter list prices (ESTIMATED), measured provider spend when a sanctioned source exists, note-taker cost separate, Timing-style HTML/PDF plus report.json, line-items.csv, evidence.json.Stars: 117kUpdated Claude CodeCodexVerifiedInstall