Have a question? Be sure to check the FAQ first!
SQLx is an async, pure Rust SQL crate that features compile-time checked queries without a DSL (Domain Specific Language).
Features
- Truly Asynchronous: Built from the ground up using async/await for maximum concurrency.
- Compile-time Checked Queries: Ensures SQL queries are checked against the database schema.
- Database Agnostic: Supports various databases including PostgreSQL, MySQL, MariaDB, and SQLite.
- Pure Rust: Drivers written in pure Rust (except for SQLite).
- Runtime Agnostic: Compatible with different runtimes like async-std, Tokio, and Actix.
- Cross-Platform: Compiles anywhere Rust is supported.
- Built-in Connection Pooling: Helps manage database connections efficiently.
- Automatic Statement Preparation and Caching: Caches prepared statements per connection.
- Transport Layer Security (TLS): Supported for several databases.
How to Install SQLx
SQLx is compatible with multiple runtimes and TLS backends. Choose a runtime feature when adding the dependency:
# Cargo.toml
[dependencies]
# PICK ONE OF THE FOLLOWING:
# tokio (no TLS)
sqlx = { version = "0.8", features = ["runtime-tokio"] }
# async-std (no TLS)
sqlx = { version = "0.8", features = ["runtime-async-std"] }
How to Use SQLx
Let’s dive into using SQLx with an analogy: think of SQLx as a skilled waiter in a trendy restaurant. Just like a waiter takes your order and communicates with the kitchen to make sure your meal is prepared exactly how you like it, SQLx helps you send queries to your database—and ensures they’re syntactically correct before it even reaches the database server!
Here’s how to create a connection pool and perform a query:
use sqlx::postgres::PgPoolOptions;
#[tokio::main]
async fn main() -> Result<(), sqlx::Error> {
// Create a connection pool
let pool = PgPoolOptions::new()
.max_connections(5)
.connect("postgres://postgres:password@localhost/test")
.await?;
// Make a simple query
let row: (i64,) = sqlx::query_as("SELECT $1")
.bind(150_i64)
.fetch_one(&pool)
.await?;
assert_eq!(row.0, 150);
Ok(())
}
Troubleshooting Tips
- Ensure your database URL is correctly formatted.
- Check if the required dependencies are included in your `Cargo.toml` file.
- If you encounter connection issues, confirm that the database server is running and accessible.
- For compile-time SQL checking, make sure your development database matches the one you are querying.
- For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
Conclusion
SQLx is a powerful tool that integrates efficiently with Rust, allowing developers to leverage Rust’s safety features while enjoying the flexibility of SQL databases.
At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.