RpRelPyDBRelational Python

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:

  1. Whole table: pass only table_name.
  2. Single column: pass column_name="...".
  3. Specific primary-key row: pass where_key={...}.
Exports are public views: internal row ids are hidden, and encrypted values are shown as [ENCRYPTED] unless decrypt=True.

Shared export parameters

ParameterAccepted byTypeMeaning
table_nameall table exportsstrThe table to export.
column_nameto_list, to_json, to_pandas, to_numpystr | NoneExport one column instead of full rows.
where_keyto_list, to_json, to_pandas, to_numpy, to_sqldict | NoneExport one row identified by primary key. Still returns a list-like result for consistency.
decryptall public exportsboolIf 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.

to_list(table_name, column_name=None, where_key=None, decrypt=False)

Returns list[dict] for rows, or list[Any] for one column. It always returns a list, even when selecting one primary-key row.

Whole table
db.to_list("users")
Output
[{"id": 1, "name": "Alice", "email": "[ENCRYPTED]"},
 {"id": 2, "name": "Bob", "email": "[ENCRYPTED]"}]
Single column
db.to_list("users", column_name="name")
Output
["Alice", "Bob"]
Specific row
db.to_list("users", where_key={"id": 1})
Output
[{"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.

to_json(table_name, column_name=None, where_key=None, *, decrypt=False, indent=2, ensure_ascii=False)
OptionUse it when
indent=2You want readable JSON.
indent=NoneYou want compact JSON.
ensure_ascii=FalseYou want Hebrew/Unicode characters to remain readable.
decrypt=TrueYou 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.

to_pandas(table_name, column_name=None, where_key=None, *, decrypt=False)
Whole table
df = db.to_pandas("users")
Output
   id   name        email
0   1  Alice  [ENCRYPTED]
1   2    Bob  [ENCRYPTED]
One column DataFrame
db.to_pandas("users", column_name="email", decrypt=True)
Output
               email
0  alice@example.com
1    bob@example.com

to_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.

to_numpy(table_name, column_name=None, where_key=None, *, decrypt=False, dtype=None)
One numeric column
db.to_numpy("orders", column_name="amount", dtype=float)
Output
array([120., 80., 50.])
Whole table
db.to_numpy("users")
Output
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.

print_table(table_name, *, limit=3, max_width=24, decrypt=False) -> None
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.

to_sql(table_name, *, row=None, where_key=None, decrypt=False) -> str
Whole table
db.to_sql("users", decrypt=True)
Output
INSERT INTO "users" ("id", "name", "email") VALUES (1, 'Alice', 'alice@example.com');
INSERT INTO "users" ("id", "name", "email") VALUES (2, 'Bob', 'bob@example.com');
Specific row
db.to_sql("users", where_key={"id": 1}, decrypt=True)
Output
INSERT INTO "users" ("id", "name", "email") VALUES (1, 'Alice', 'alice@example.com');
Encrypted columns: if a table contains encrypted columns, to_sql(..., decrypt=True) is required because SQL output must contain real runnable values, not [ENCRYPTED].