Skip to Content
This documentation is still under construction
Rust APIConnect

Connecting to a database in Rust

Enable the feature for your database engine on the alphadb crate. In this example we’ll use PostgreSQL.

[dependencies] alphadb = { version = "...", features = ["postgres"] }

Import AlphaDB and the engines module.

use alphadb::AlphaDB; use alphadb::engine;

Create an instance with the engine’s runtime config and connect it to a database using your credentials.

let mut db = AlphaDB::new(engine::postgres()); match db.connect("host", "user", "password", "database", 5432) { Ok(_) => println!("Connected!"), Err(e) => eprintln!("Connection attempt failed: {}", e.message) }

To connect to MySQL instead, enable the mysql feature and use engine::mysql(), with the matching port (usually 3306).

Replace the credentials with your own. AlphaDB requires the user to have the following privileges: CREATE, INSERT, DELETE, SELECT, UPDATE, ALTER, DROP and REFERENCES.

Last updated on