> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-mintlify-fbfa8bee.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Installing chDB for Rust

> How to install and use chDB Rust bindingsd

chDB-rust provides experimental FFI (Foreign Function Interface) bindings for chDB, enabling you to run ClickHouse queries directly in your Rust applications with zero external dependencies.

<h2 id="installation">
  Installation
</h2>

<h3 id="install-libchdb">
  Install libchdb
</h3>

Install the chDB library:

```bash theme={null}
curl -sL https://lib.chdb.io | bash
```

<h2 id="usage">
  Usage
</h2>

chDB Rust provides both stateless and stateful query execution modes.

<h3 id="stateless-usage">
  Stateless usage
</h3>

For simple queries without persistent state:

```rust theme={null}
use chdb_rust::{execute, arg::Arg, format::OutputFormat};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Execute a simple query
    let result = execute(
        "SELECT version()",
        Some(&[Arg::OutputFormat(OutputFormat::JSONEachRow)])
    )?;
    println!("ClickHouse version: {}", result.data_utf8()?);
    
    // Query with CSV file
    let result = execute(
        "SELECT * FROM file('data.csv', 'CSV')",
        Some(&[Arg::OutputFormat(OutputFormat::JSONEachRow)])
    )?;
    println!("CSV data: {}", result.data_utf8()?);
    
    Ok(())
}
```

<h3 id="stateful-usage-sessions">
  Stateful usage (Sessions)
</h3>

For queries requiring persistent state like databases and tables:

```rust theme={null}
use chdb_rust::{
    session::SessionBuilder,
    arg::Arg,
    format::OutputFormat,
    log_level::LogLevel
};
use tempdir::TempDir;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create a temporary directory for database storage
    let tmp = TempDir::new("chdb-rust")?;
    
    // Build session with configuration
    let session = SessionBuilder::new()
        .with_data_path(tmp.path())
        .with_arg(Arg::LogLevel(LogLevel::Debug))
        .with_auto_cleanup(true)  // Cleanup on drop
        .build()?;

    // Create database and table
    session.execute(
        "CREATE DATABASE demo; USE demo", 
        Some(&[Arg::MultiQuery])
    )?;

    session.execute(
        "CREATE TABLE logs (id UInt64, msg String) ENGINE = MergeTree() ORDER BY id",
        None,
    )?;

    // Insert data
    session.execute(
        "INSERT INTO logs (id, msg) VALUES (1, 'Hello'), (2, 'World')",
        None,
    )?;

    // Query data
    let result = session.execute(
        "SELECT * FROM logs ORDER BY id",
        Some(&[Arg::OutputFormat(OutputFormat::JSONEachRow)]),
    )?;

    println!("Query results:\n{}", result.data_utf8()?);
    
    // Get query statistics
    println!("Rows read: {}", result.rows_read());
    println!("Bytes read: {}", result.bytes_read());
    println!("Query time: {:?}", result.elapsed());

    Ok(())
}
```

<h2 id="building-testing">
  Building and testing
</h2>

<h3 id="build-the-project">
  Build the project
</h3>

```bash theme={null}
cargo build
```

<h3 id="run-tests">
  Run tests
</h3>

```bash theme={null}
cargo test
```

<h3 id="development-dependencies">
  Development dependencies
</h3>

The project includes these development dependencies:

* `bindgen` (v0.70.1) - Generate FFI bindings from C headers
* `tempdir` (v0.3.7) - Temporary directory handling in tests
* `thiserror` (v1) - Error handling utilities

<h2 id="error-handling">
  Error handling
</h2>

chDB Rust provides comprehensive error handling through the `Error` enum:

```rust theme={null}
use chdb_rust::{execute, error::Error};

match execute("SELECT 1", None) {
    Ok(result) => {
        println!("Success: {}", result.data_utf8()?);
    },
    Err(Error::QueryError(msg)) => {
        eprintln!("Query failed: {}", msg);
    },
    Err(Error::NoResult) => {
        eprintln!("No result returned");
    },
    Err(Error::NonUtf8Sequence(e)) => {
        eprintln!("Invalid UTF-8: {}", e);
    },
    Err(e) => {
        eprintln!("Other error: {}", e);
    }
}
```

<h2 id="github-repository">
  GitHub repository
</h2>

You can find the GitHub repository for the project at [chdb-io/chdb-rust](https://github.com/chdb-io/chdb-rust).
