Documentation
11. Exports
Export tables, columns and specific rows to Python, JSON, pandas, NumPy, terminal tables and SQL INSERT statements.
Export mental model
Most export functions support three selection modes:
- Whole table: pass only
table_name. - Single column: pass
column_name="...". - Specific primary-key row: pass
where_key={...}.
[ENCRYPTED] unless decrypt=True.Shared export parameters
| Parameter | Accepted by | Type | Meaning |
|---|---|---|---|
table_name | all table exports | str | The table to export. |
column_name | to_list, to_json, to_pandas, to_numpy | str | None | Export one column instead of full rows. |
where_key | to_list, to_json, to_pandas, to_numpy, to_sql | dict | None | Export one row identified by primary key. Still returns a list-like result for consistency. |
decrypt | all public exports | bool | If True, decrypt encrypted values using the loaded key. |
Export Lab
Choose a function to see table/column/row examples and outputs.
to_list
to_list() is the best export when you want native Python values.
Returns list[dict] for rows, or list[Any] for one column. It always returns a list, even when selecting one primary-key row.
db.to_list("users")[{"id": 1, "name": "Alice", "email": "[ENCRYPTED]"},
{"id": 2, "name": "Bob", "email": "[ENCRYPTED]"}]db.to_list("users", column_name="name")["Alice", "Bob"]db.to_list("users", where_key={"id": 1})[{"id": 1, "name": "Alice", "email": "[ENCRYPTED]"}]to_json
to_json() converts the same public export semantics into a JSON string. It is for public/export JSON, not for restoring a full RelPyDB object. Use save() for that.
| Option | Use it when |
|---|---|
indent=2 | You want readable JSON. |
indent=None | You want compact JSON. |
ensure_ascii=False | You want Hebrew/Unicode characters to remain readable. |
decrypt=True | You explicitly want plaintext encrypted values in the JSON export. |
db.to_json("users", where_key={"id": 1}, indent=2)[
{
"id": 1,
"name": "Alice",
"email": "[ENCRYPTED]"
}
]to_pandas
to_pandas() returns a DataFrame and is best for analysis or notebooks.
df = db.to_pandas("users") id name email
0 1 Alice [ENCRYPTED]
1 2 Bob [ENCRYPTED]db.to_pandas("users", column_name="email", decrypt=True) email
0 alice@example.com
1 bob@example.comto_numpy
to_numpy() returns an array. With column_name, it returns a 1D array. Without it, it returns a 2D array in schema column order.
db.to_numpy("orders", column_name="amount", dtype=float)array([120., 80., 50.])db.to_numpy("users")array([[1, 'Alice', '[ENCRYPTED]'],
[2, 'Bob', '[ENCRYPTED]']], dtype=object)print_table
print_table() is a debugging helper. It prints a human-readable preview instead of returning data.
db.print_table("users")Table: users (showing 2 of 2 rows)
┌─────────────────┬────────────┬────────────────────────┐
│ id (AutoNumber) │ name (str) │ email (str, encrypted) │
├─────────────────┼────────────┼────────────────────────┤
│ 1 │ Alice │ [ENCRYPTED] │
│ 2 │ Bob │ [ENCRYPTED] │
└─────────────────┴────────────┴────────────────────────┘Use limit to avoid printing too much data. Use max_width for long text fields.
to_sql
to_sql() generates runnable INSERT INTO statements for an SQL table that already exists. It does not create the table.
db.to_sql("users", decrypt=True)INSERT INTO "users" ("id", "name", "email") VALUES (1, 'Alice', 'alice@example.com');
INSERT INTO "users" ("id", "name", "email") VALUES (2, 'Bob', 'bob@example.com');db.to_sql("users", where_key={"id": 1}, decrypt=True)INSERT INTO "users" ("id", "name", "email") VALUES (1, 'Alice', 'alice@example.com');to_sql(..., decrypt=True) is required because SQL output must contain real runnable values, not [ENCRYPTED].