Created
January 26, 2026 16:01
-
-
Save jdmichaud/ed7dbac55358f8136a474ecd4470fed4 to your computer and use it in GitHub Desktop.
Rust sqlite easy query
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use rusqlite::{Connection, Result, Params}; | |
| pub struct Query<'conn> { | |
| stmt: rusqlite::CachedStatement<'conn>, | |
| } | |
| impl<'conn> Query<'conn> { | |
| pub fn new(conn: &'conn Connection, sql: &str) -> Result<Self> { | |
| Ok(Self { stmt: conn.prepare_cached(sql)? }) | |
| } | |
| pub fn run<P: Params>(&mut self, params: P) -> Result<()> { | |
| self.stmt.execute(params)?; | |
| Ok(()) | |
| } | |
| } | |
| // Usage: | |
| let mut query = Query::new(&conn, "INSERT INTO nodes (id, lat, lon) VALUES (?1, ?2, ?3)")?; | |
| query.run((1, 2.0, 3.0))?; | |
| query.run((4, 5.0, 6.0))?; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment