include-sqlite-sql is an extension of include-sql for using SQLite SQL in Rust. It completes include-sql by providing impl_sql
macro to generate database access methods from the included SQL. include-sqlite-sql uses Rusqlite for database access.
Write your SQL and save it in a file. For example, let's say the following is saved as library.sql
in the project's src
folder:
```sql
SELECT booktitle FROM library WHERE loanedto = :user_id ORDER BY 1
-- param: book_titles: &str - book titles
UPDATE library SET loanedto = :userid , loanedon = currenttimestamp WHERE booktitle IN (:booktitles) ```
And then use it in Rust as:
```rust , ignore use includesqlitesql::{includesql, implsql}; use rusqlite::{Result, Connection};
include_sql!("src/library.sql");
fn main() -> Result<()> { let db = Connection::open("library.db")?;
db.loan_books(&["War and Peace", "Gone With the Wind"], "Sheldon Cooper")?;
db.get_loaned_books("Sheldon Cooper", |row| {
let book_title : &str = row.get_ref(0)?.as_str()?;
println!("{book_title}");
Ok(())
})?;
Ok(())
} ```
The included documentation describes the supported SQL file format and provides additional details on the generated code.
;
to /
. SQL files that used ;
terminator would need to change it to /
or remove it completely.&
. See library.sql for an example of its use.