# How to Export Query Results from DBX in CSV, JSON, XLSX, and Markdown

> Learn to export DBX query results in CSV, JSON, XLSX, and Markdown formats. Utilize Tauri commands for seamless serialization from Rust backend to disk via UI or CLI.

- Repository: [skyler/dbx](https://github.com/t8y2/dbx)
- Tags: how-to-guide
- Published: 2026-07-04

---

**DBX provides dedicated Tauri commands for each format—`export_query_result_csv`, `export_query_result_json`, `export_query_result_markdown`, and `export_query_result_xlsx`—allowing you to serialize query results from the Rust backend directly to disk via the desktop UI or CLI.**

DBX is an open-source database client built with a Tauri-based desktop backend. Whether you are scripting data pipelines or analyzing results in the GUI, you can export query results from DBX into four industry-standard formats using native Rust serialization commands that separate formatting logic from I/O operations.

## How the Export Architecture Works

DBX separates formatting logic from file I/O through a clean Rust-core-to-Tauri-bridge pattern. When you execute a query, `dbx_core::database::execute_query` returns a `QueryResult` struct containing columns and rows. The Tauri backend then exposes format-specific commands that serialize this data and write it to your chosen file path with UTF-8 BOM encoding.

The data flow follows four steps:

1. **Query execution** returns a `QueryResult` (`columns: Vec<String>`, `rows: Vec<Vec<Value>>`).
2. **Frontend handling** passes results to export helpers in [`apps/desktop/src/lib/backend/tauri.ts`](https://github.com/t8y2/dbx/blob/main/apps/desktop/src/lib/backend/tauri.ts).
3. **Tauri invocation** marshals the request to Rust commands like `export_query_result_csv` or `export_query_result_json`.
4. **File writing** occurs in the respective command files (e.g., [`src-tauri/src/commands/csv_export.rs`](https://github.com/t8y2/dbx/blob/main/src-tauri/src/commands/csv_export.rs)), outputting formatted content to disk.

## Exporting Query Results from DBX via the CLI

For automation and scripting, the DBX CLI supports format selection via the `--format` flag. Parsing logic resides in [`packages/cli/src/cli.ts`](https://github.com/t8y2/dbx/blob/main/packages/cli/src/cli.ts), which forwards requests to the corresponding Tauri backend commands.

```bash

# CSV (default table view)

dbx query local "SELECT * FROM users" --format csv > users.csv

# JSON

dbx query local "SELECT * FROM users" --format json > users.json

# XLSX (requires --output flag)

dbx query local "SELECT * FROM users" --format xlsx --output users.xlsx

# Markdown (ideal for README tables)

dbx query local "SELECT * FROM users" --format markdown > users.md

```

## Exporting Query Results from DBX in the Desktop UI

When building custom UI components or extensions, import the export wrappers from [`apps/desktop/src/lib/backend/tauri.ts`](https://github.com/t8y2/dbx/blob/main/apps/desktop/src/lib/backend/tauri.ts). These TypeScript functions invoke the Rust commands asynchronously and handle file path resolution.

```typescript
import {
  exportQueryResultCsv,
  exportQueryResultXlsx,
  exportQueryResultJson,
  exportQueryResultMarkdown,
} from '@/lib/backend/tauri';

const columns = ['id', 'name', 'email'];
const rows = [
  [1, 'Ada', 'ada@example.com'],
  [2, 'Bob', 'bob@example.com'],
];

// Export to CSV
await exportQueryResultCsv('/tmp/users.csv', columns, rows);

// Export to XLSX (single sheet with custom name)
await exportQueryResultXlsx('/tmp/users.xlsx', 'Users', columns, rows);

// Export to JSON
await exportQueryResultJson('/tmp/users.json', columns, rows);

// Export to Markdown
await exportQueryResultMarkdown('/tmp/users.md', columns, rows);

```

## Exporting Selected Rows from the Data Grid

To export only a user-selected subset rather than the full result set, use the `useDataGridExport` composable. This utility packages selected rows and invokes the appropriate backend command based on the requested format.

```typescript
import { useDataGridExport } from '@/composables/useDataGridExport';

const { exportQueryResultViaBackend } = useDataGridExport();

// Export only selected rows to CSV
await exportQueryResultViaBackend('csv');

// Export selection to XLSX
await exportQueryResultViaBackend('xlsx');

```

The composable logic resides in [`apps/desktop/src/composables/useDataGridExport.ts`](https://github.com/t8y2/dbx/blob/main/apps/desktop/src/composables/useDataGridExport.ts).

## Core Rust Implementation Files

The formatting logic for each export type lives in dedicated Rust source files within the `src-tauri/src/commands/` directory:

- **CSV**: [`src-tauri/src/commands/csv_export.rs`](https://github.com/t8y2/dbx/blob/main/src-tauri/src/commands/csv_export.rs) implements `format_query_result_csv`, handling delimiter separation and UTF-8 BOM headers.
- **JSON & Markdown**: [`src-tauri/src/commands/text_export.rs`](https://github.com/t8y2/dbx/blob/main/src-tauri/src/commands/text_export.rs) contains `format_json` and `format_markdown` for structured text output.
- **XLSX**: [`src-tauri/src/commands/xlsx_export.rs`](https://github.com/t8y2/dbx/blob/main/src-tauri/src/commands/xlsx_export.rs) leverages `dbx_core::xlsx_export` to generate Excel workbooks with proper column formatting.

This architecture ensures that [`apps/desktop/src/lib/backend/tauri.ts`](https://github.com/t8y2/dbx/blob/main/apps/desktop/src/lib/backend/tauri.ts) remains a thin wrapper, while the heavy serialization logic stays testable and reusable across the desktop app and CLI.

## Summary

- **DBX** ships four Tauri commands—`export_query_result_csv`, `export_query_result_json`, `export_query_result_markdown`, and `export_query_result_xlsx`—to handle serialization.
- The **CLI** parses `--format` arguments in [`packages/cli/src/cli.ts`](https://github.com/t8y2/dbx/blob/main/packages/cli/src/cli.ts) and streams output to stdout or files.
- The **desktop UI** uses TypeScript wrappers in [`apps/desktop/src/lib/backend/tauri.ts`](https://github.com/t8y2/dbx/blob/main/apps/desktop/src/lib/backend/tauri.ts) to invoke Rust commands asynchronously.
- **Selected rows** can be exported via the `useDataGridExport` composable without re-querying the database.
- All exports include **UTF-8 BOM** encoding and are implemented in pure Rust within `src-tauri/src/commands/`, ensuring consistent performance across platforms.

## Frequently Asked Questions

### Can I export query results from DBX using the command line?

Yes. The DBX CLI supports the `--format` flag with options for `csv`, `json`, `xlsx`, and `markdown`. For XLSX exports, you must specify an `--output` file path, while other formats write to stdout for redirection.

### Does DBX support exporting only selected rows from the data grid?

Yes. The `useDataGridExport` composable in [`apps/desktop/src/composables/useDataGridExport.ts`](https://github.com/t8y2/dbx/blob/main/apps/desktop/src/composables/useDataGridExport.ts) provides `exportQueryResultViaBackend()`, which packages only the selected rows and passes them to the appropriate Tauri command without re-executing the query.

### What file formats does DBX support for exporting query results?

DBX natively supports **CSV**, **JSON**, **XLSX** (Excel), and **Markdown** table formats. Each format has a dedicated Rust implementation file in `src-tauri/src/commands/` (e.g., [`csv_export.rs`](https://github.com/t8y2/dbx/blob/main/csv_export.rs) for CSV and [`text_export.rs`](https://github.com/t8y2/dbx/blob/main/text_export.rs) for JSON/Markdown).

### How does DBX handle encoding for CSV exports?

According to the `t8y2/dbx` source code, CSV exports are written with **UTF-8 BOM** (Byte Order Mark) encoding. This ensures proper character recognition when opening files in spreadsheet applications like Microsoft Excel.