How to save ready-made SQLite statements for later versions?

Right now I have a code that uses rusqlitesqlite bindings to open a db connection and perform a bunch of db operations in my application for example:

extern crate rusqlite;

use rusqlite::SqliteConnection;

struct MyAppState {
    db: SqliteConnection,
    // ... pretend there other fields here ...
}

impl MyAppState {
    fn new() -> MyAppState {
        let db = SqliteConnection::open(":memory:").unwrap();
        MyAppState {
            db: db
        }
    }

    fn query_some_info(&mut self, arg: i64) -> i64 {
        let mut stmt = self.db.prepare("SELECT ? + 1").unwrap();
        let mut result_iter = stmt.query(&[&arg]).unwrap();
        let result = result_iter.next().unwrap().unwrap().get(0);

        result
    }
}

fn main() {
    let mut app = MyAppState::new();
    for i in range(0, 100) {
        let result = app.query_some_info(i);
        println!("{}", result);
    }
}

Since the prepared statement lives in a local variable, this seems to miss the point of the prepared statements to some extent, since I have to rework it every time the function is called and a local variable occurs. Ideally, I would have prepared all my statements as much as possible and ran them in the structure MyAppStatefor the duration of the db connection.

, SqliteStatement db, struct it , , &mut self (query_some_info &mut self , , RefCell s, , , ).

, , , Rc<RefCell< >> , , , , .

, , db , , , , , prepare db, rusqlite, API sqlite3 C - . ?

+4
1

, , Rust. , .

: , db, ; db ( ), , , .

, db , .

+4

Source: https://habr.com/ru/post/1568190/


All Articles