[−][src]Macro sqlx::query_file
This is supported on
feature="macros"
only.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.
examples/queries/account-by-id.sql
:
select * from (select (1) as id, 'Herp Derpinson' as name) accounts
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);