Crate duckdb[][src]

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 arrow::record_batch::RecordBatch;
use 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

pub use libduckdb_sys as ffi;

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.

An handle for the resulting RecordBatch of a query.

Information about a column of a DuckDB query.

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.

Flags for opening DuckDB database connections. NOTE: Currently we don’t support READ_ONLY mode

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

Name for a database within a DuckDB connection.

Options for how a Transaction or Savepoint should behave when it is dropped.

Enum listing possible errors from duckdb.

Error Codes

Options for transaction behavior. See BEGIN TRANSACTION for details.

Constants

Traits

Trait used for sets of parameter passed into SQL statements/queries.

A trait implemented by types that can index into columns of a row.

A trait for types that can be converted into DuckDB values. Returns [Error::ToSqlConversionFailure] if the conversion fails.

Functions

Constructor function for a ParamsFromIter. See its documentation for more.

Type Definitions

A typedef of the result returned by many methods.