matlab/matlab-agentic-toolkit / skills-catalog/reporting-and-database-access/matlab-use-database
matlab-use-database
VerifiedReads 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.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 12.3 KB |
| manifest.yaml | config | 491 B |
| references/execute-storedproc.md | markdown | 3.4 KB |
| references/multithreaded-io.md | markdown | 6.7 KB |
| references/orm.md | markdown | 3.9 KB |
| references/sqlread-fetch.md | markdown | 4.2 KB |
| references/sqlwrite-sqlupdate.md | markdown | 2.7 KB |
| references/transactions.md | markdown | 2.0 KB |
references/orm.md
View on GitHub# ORM — Object Relational Mapping (R2023b+)
## Defining a Mappable Class
```matlab
classdef Employee < database.orm.mixin.Mappable
properties (PrimaryKey)
EmployeeID int32
end
properties
Name string
Department string
Salary double
end
properties (ColumnType = "date")
HireDate datetime
end
methods
function obj = Employee(id, name, dept, salary, hireDate)
if nargin == 0
return;
end
obj.EmployeeID = id;
obj.Name = name;
obj.Department = dept;
obj.Salary = salary;
obj.HireDate = hireDate;
end
function obj = promote(obj, raise)
obj.Salary = obj.Salary + raise;
end
end
end
```
## Requirements
- Inherit from `database.orm.mixin.Mappable`
- Mark at least one property block `(PrimaryKey)`
- Constructor must handle `nargin == 0` (ORM constructs empty objects internally)
- Save class in its own `.m` file (one class per file)
- R2023b or later
## Optional Property Attributes
| Attribute | Purpose | Example |
|-----------|---------|---------|
| `PrimaryKey` | Marks property as the primary key (required) | `properties (PrimaryKey)` |
| `ColumnName` | Maps property to a differently-named column | `properties (ColumnName = "EmpID")` |
| `ColumnType` | Specifies the database column type | `properties (ColumnType = "date")` |
| `TableName` | Class-level: maps class to a differently-named table | `classdef (TableName = "emp") Employee` |
`TableName` defaults to the class name — only specify it when the database table name differs.
## CRUD Operations
### Write
```matlab
emp = Employee(1, "Alice", "Engineering", 95000, datetime(2024,3,15));
ormwrite(conn, emp);
% Write an array of objects
emps = [Employee(2, "Bob", "Sales", 72000, datetime(2023,6,1)), ...
Employee(3, "Carol", "Engineering", 105000, datetime(2022,1,10))];
ormwrite(conn, emps);
```
### Read
```matlab
% Read all objects
allEmployees = ormread(conn, "Employee");
% Read with a filter
rf = rowfilter("Salary");
highEarners = ormread(conn, "Employee", RowFilter=rf.Salary > 90000);
% Refresh an existing object from database
emp = ormread(conn, emp);
```
### Update
```matlab
emp = promote(emp, 5000);
ormupdate(conn, emp);
```
### Delete
ORM does not provide `ormdelete`. Use `execute` for deletion:
```matlab
execute(conn, "DELETE FROM Employee WHERE EmployeeID = 42");
```
### Generate SQL from Class
```matlab
sql = orm2sql(conn, "Employee");
disp(sql);
```
## When to Use ORM vs sqlread/sqlwrite
| Scenario | Use ORM | Use sqlread/sqlwrite |
|----------|---------|---------------------|
| Object identity and business logic | Yes | No |
| Class-based type safety | Yes | No |
| Domain validation on read/write | Yes | No |
| Ad-hoc queries or exploration | No | Yes |
| Bulk operations (thousands of rows) | No | Yes (faster) |
| No object mapping needed | No | Yes |
## Error Handling
```matlab
try
ormwrite(conn, emp);
catch ME
if contains(ME.message, "UNIQUE") || contains(ME.message, "primary key")
warning("Duplicate key. Using ormupdate instead.");
ormupdate(conn, emp);
else
rethrow(ME);
end
end
```
## Supported Connections
ORM works with: JDBC, ODBC, PostgreSQL native, MySQL native, SQLite native, DuckDB native.
ORM does NOT work with: MongoDB, Cassandra, Neo4j.
## Gotchas
- `ormwrite` errors on duplicate primary keys — use try/catch to fall back to `ormupdate`
- `ormupdate` matches rows by primary key — the object's PK must exist in the database
- `ormread` returns an empty array if no rows match — check with `isempty`
- Use `orm2sql` to verify the class mapping matches your database schema before writing
- ORM is slower than `sqlwrite`/`sqlread` for bulk operations — use functional CRUD for large batches
----
Copyright 2026 The MathWorks, Inc.
----
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| 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: 119k | Updated | Claude CodeCodex | Verified | Install |
| 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: 119k | Updated | Claude CodeCodex | Verified | Install |
| 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: 118k | Updated | Claude CodeCodex | Verified | Install |
| 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: 118k | Updated | Claude CodeCodex | Verified | Install |