pub struct Database { /* private fields */ }Expand description
A shared handle to an open DuckDB database.
Cloning a Database is cheap (an Arc bump) and does not touch DuckDB. Use
connect to spawn independent Connections that share
this database — including, for :memory: databases, connections that observe
the same data.
This is different from calling Connection::open_in_memory multiple times,
which gives each connection its own independent in-memory database:
use better_duck_core::{connection::Connection, database::Database};
// Shared: both connections see the same in-memory data.
let db = Database::open_in_memory().expect("open database");
let mut a = db.connect().expect("connect");
let mut b = db.connect().expect("connect");
a.execute_batch("CREATE TABLE t (id INTEGER)").expect("create table");
b.execute_batch("INSERT INTO t VALUES (1)").expect("insert");
// Independent: each has its own in-memory database.
let mut x = Connection::open_in_memory().expect("open");
let mut y = Connection::open_in_memory().expect("open");Implementations§
Source§impl Database
impl Database
Sourcepub fn open<P: AsRef<Path>>(path: P) -> Result<Database>
pub fn open<P: AsRef<Path>>(path: P) -> Result<Database>
Opens a database at the given file path.
§Errors
Returns an error if the database cannot be opened or the path contains a nul byte.
Sourcepub fn open_with_flags<P: AsRef<Path>>(
path: P,
config: Config,
) -> Result<Database>
pub fn open_with_flags<P: AsRef<Path>>( path: P, config: Config, ) -> Result<Database>
Opens a database at the given file path with additional config.
§Errors
Returns an error if the database cannot be opened or the path contains a nul byte.
Sourcepub fn open_in_memory() -> Result<Database>
pub fn open_in_memory() -> Result<Database>
Sourcepub fn open_in_memory_with_flags(config: Config) -> Result<Database>
pub fn open_in_memory_with_flags(config: Config) -> Result<Database>
Opens an in-memory database with additional config.
§Errors
Returns an error if the database cannot be opened.
Sourcepub fn connect(&self) -> Result<Connection>
pub fn connect(&self) -> Result<Connection>
Opens a new connection to this database.
This is a single duckdb_connect call — no file I/O. All connections opened
from this Database (or its clones) share the same underlying data.
§Errors
Returns an error if the connection cannot be established.
Source§impl Database
impl Database
Sourcepub fn register_replacement_scan<T: ReplacementScan>(&self)
pub fn register_replacement_scan<T: ReplacementScan>(&self)
Registers a replacement scan: T::replace is called whenever DuckDB
can’t resolve a table reference to an existing table, view, or CTE, on
every connection to this database (replacement scans are scoped
per-database, not per-connection).
T carries no instance data — like VTab/
VScalar, it’s used purely as a marker type; the
DuckDB C API itself has no failure mode for this registration.