Sqlite: caching prepared statements .... again

Well you need the connection as well since the statement is tied to it.

An approach I’d suggest is what you said later:

You’d set up your connections early on in main(), prepare statements that you know a priori, and then hand out the statements as needed. These instances would be kept around (and live) for the entirety of the program and passed around as context/state where needed.

You mentioned gtk in the other post - does it require closures that you give it to be 'static? If so, yeah, you’ll need to use Rc and RefCell. This should be workable but you have to align your code structure/flow with easier borrow checking. It’s a bit of a skill (comes with Rust experience) in itself. I wrote a bit about some general strategies here: How can I get myself out of this "mutable more than once" corner I've backed myself into?. Maybe that’ll help frame your approach a bit better.

By the way, have you looked at any of the connection pooling crates? Eg r2d2_sqlite3 - Rust

I think the sqlite API says the correct thing about the relationship. A statement depends on the connection you get it from. That seems pretty uncontroversial.

Lifetimes are not easy, but I think the friction you’re experiencing here is because of trying to shoehorn dynamic data into a 'static one. I’d be happy to try to explain some lifetime issues if you’d like - not sure if I’ll succeed but worth a shot :slight_smile:.