Talking to a database
SQLx checks your SQL at compile time against a real database, which is either the best thing about Rust’s database story or the most annoying, depending on the day.
From the connection pool to the integration test, this is the whole path — including the injection mistake everybody writes once.
Articles
Section titled “Articles”- How to connect to Postgresthe pool, and the DATABASE_URL you will write wrong
- How to run a query and read the rowsquery, fetch_all, and mapping into a struct
- How to manage a schema with migrationssqlx migrate, and why the file is plain SQL
- How to get queries checked at compile timethe query! macro and sqlx prepare in CI
- How to insert a row and get its idRETURNING, and the last_insert_rowid trap
- How to use a transactionbegin, commit, and the rollback on the error path
- How to use SQLite in a Rust programrusqlite, one file, no server
- How to map rows to structsFromRow, and the column type that does not match
- How to avoid SQL injectionbind parameters, and the format! that everyone writes once
- How to test against a real databasea schema per test, and sqlx::test