[−][src]Macro sqlx::query_file
A variant of query! where the SQL query is stored in a separate file.
Useful for large queries and potentially cleaner than multiline strings.
The syntax and requirements (see query!) are the same except the SQL string is replaced by a file path.
The file must be relative to the project root (the directory containing Cargo.toml
),
unlike include_str!()
which uses compiler internals to get the path of the file where it
was invoked.
queries/account-by-id.sql
:
SELECT * from (VALUES(1, 'Herp Derpinson')) accounts(id, name) where id != ?;
src/my_query.rs
:
// let mut conn = <impl sqlx::Executor>; let account = sqlx::query_file!("examples/queries/account-by-id.sql", 1i32) .fetch_one(&mut conn) .await?; println!("{:?}", account); println!("{}: {}", account.id, account.name);