Crate duckdb

source ·
Expand description

duckdb-rs is an ergonomic wrapper for using DuckDB from Rust. It attempts to expose an interface similar to rusqlite.

use duckdb::{params, Connection, Result};
use duckdb::arrow::record_batch::RecordBatch;
use duckdb::arrow::util::pretty::print_batches;

#[derive(Debug)]
struct Person {
    id: i32,
    name: String,
    data: Option<Vec<u8>>,
}

fn main() -> Result<()> {
    let conn = Connection::open_in_memory()?;

    conn.execute_batch(
        r"CREATE SEQUENCE seq;
          CREATE TABLE person (
                  id              INTEGER PRIMARY KEY DEFAULT NEXTVAL('seq'),
                  name            TEXT NOT NULL,
                  data            BLOB
                  );
         ")?;
    let me = Person {
        id: 0,
        name: "Steven".to_string(),
        data: None,
    };
    conn.execute(
        "INSERT INTO person (name, data) VALUES (?, ?)",
        params![me.name, me.data],
    )?;

    let mut stmt = conn.prepare("SELECT id, name, data FROM person")?;
    let person_iter = stmt.query_map([], |row| {
        Ok(Person {
            id: row.get(0)?,
            name: row.get(1)?,
            data: row.get(2)?,
        })
    })?;

    for person in person_iter {
        println!("Found person {:?}", person.unwrap());
    }

    // query table by arrow
    let rbs: Vec<RecordBatch> = stmt.query_arrow([])?.collect();
    print_batches(&rbs);
    Ok(())
}

Re-exports

Modules

  • Traits dealing with DuckDB data types.

Macros

  • A macro making it more convenient to pass heterogeneous or long lists of parameters as a &[&dyn ToSql].

Structs

  • An iterator over the mapped resulting rows of a query, with an Error type unifying with Error.
  • Appender for fast import data
  • Adapter type which allows any iterator over ToSql values to implement [Params].
  • An handle for the resulting RecordBatch of a query.
  • Cacheable statement.
  • Information about a column of a DuckDB query.
  • duckdb configuration Refer to https://github.com/duckdb/duckdb/blob/master/src/main/config.cpp
  • A connection to a DuckDB database.
  • F is used to transform the streaming iterator into a fallible iterator.
  • An iterator over the mapped resulting rows of a query.
  • Adapter type which allows any iterator over ToSql values to implement Params.
  • A single result row of a query.
  • An handle for the resulting rows of a query.
  • Represents a savepoint on a database connection.
  • A prepared statement.
  • Represents a transaction on a database connection.

Enums

Constants

Traits

Functions

Type Definitions

  • A typedef of the result returned by many methods.