151 releases (29 stable)

new 10.0.0 Nov 24, 2024
9.0.0 Oct 26, 2024
8.1.4 Aug 14, 2024
8.1.2 Jul 14, 2024
0.10.3 Nov 21, 2020

#38 in Database interfaces

Download history 9016/week @ 2024-08-04 11651/week @ 2024-08-11 10860/week @ 2024-08-18 10422/week @ 2024-08-25 6790/week @ 2024-09-01 14224/week @ 2024-09-08 9852/week @ 2024-09-15 11433/week @ 2024-09-22 9249/week @ 2024-09-29 9908/week @ 2024-10-06 7483/week @ 2024-10-13 12301/week @ 2024-10-20 7979/week @ 2024-10-27 7011/week @ 2024-11-03 5658/week @ 2024-11-10 8951/week @ 2024-11-17

30,159 downloads per month
Used in 16 crates (11 directly)

MIT license

565KB
8K SLoC

ODBC-API

Docs Licence Crates.io Coverage Status

Rust ODBC bindings. ODBC (Open Database Connectivity) is an open standard to connect to a variaty of data sources. Most data sources offer ODBC drivers. There are two typical usage scenarios for ODBC:

  • Your appliciation is designed to work with a wide range of Databases without the need of redeploying it.
  • There is no native Rust driver in suitable quality available for your Database so you want to use the ODBC driver instead.

See also: Is ODBC the Answer?

This crate is currently tested against:

  • Microsoft SQL Server
  • PostgreSQL
  • MariaDB
  • SQLite

Current ODBC Version is 3.80.

This crate is build on top of the odbc-sys ffi bindings, which provide definitions of the ODBC C Interface, but do not build any kind of abstraction on top of it.

Usage

Check the guide for code examples and a tour of the features.

Installation

To build this library you need to link against the odbc library of your systems ODBC driver manager. It should be automatically detected by the build. On Windows systems it is preinstalled. On Linux and OS-X unix-odbc must be installed. To create a Connections to a data source, its ODBC driver must also be installed.

Windows

Nothing to do. ODBC driver manager is preinstalled.

Ubuntu

sudo apt-get install unixodbc-dev

OS-X (intel)

You can use homebrew to install UnixODBC

brew install unixodbc

OS-X (ARM / MAC M1)

cargo build is not going to pick up libodbc.so installed via homebrew due to the fact that homebrew on ARM Mac installs into /opt/homebrew/Cellar as opposed to /usr/local/opt/.

You find documentation on what directories are searched during build here: https://doc.rust-lang.org/cargo/reference/environment-variables.html#dynamic-library-paths.

You can also install unixODBC from source:

  1. copy the unixODBC-2.3.9.tar.gz file somewhere you can create files and directories
  2. gunzip unixODBC*.tar.gz
  3. tar xvf unixODBC*.tar
  4. ./configure
  5. make
  6. make install

Linux / OS-X via Nix Flakes

Alternatively, if you're familiar with the Nix ecosystem or want to try it out, our wiki has a user contributed example on setting up a dev environment with Nix flakes.

Features

  • Connect using Data Source names (DSN)
  • Connect using ODBC connection strings
  • Connect using prompts (windows)
  • Log ODBC diagnostics and warnings (via log crate).
  • Columnar bulk inserts.
  • Columnar bulk queries.
  • Rowise bulk inserts.
  • Rowise bulk queries.
  • Output parameters of stored procedures.
  • prepared and 'one shot' queries.
  • Transactions
  • Pass parameters to queries
  • Asynchronous execution of one shot queries
  • Asynchronous execution of prepared queries
  • Asynchronous connecting to database
  • Asynchronous fetching of Metainformation
  • Multithreading
  • Inserting large binary / text data in stream
  • Fetching arbitrary large text / binary data in stream
  • Connection pooling
  • List tables of data sources
  • Retrieve multiple result sets

Dependencies

~1–19MB
~215K SLoC