The SQLite prepared statements seems like a good way to execute queries versus raw sql statements. I’m just wondering how long they should be kept around.
Is it safe to statically compile the statement, i.e. only compile it once and keep using the same statement for many queries?
If this is not thread safe then what about on a thread local?
Conversely are prepared statements still preferred if you only use them once?
The advantages of Prepared Statements are:
As the execution plan get cached, performance will be better.
It is a good way to code against SQL Injection as escapes the input
values.
When it comes to a Statement with no unbound variables, the database
is free to optimize to its full extent. The individual query will be
faster, but the down side is that you need to do the database
compilation all the time, and this is worse than the benefit of the
faster query.
For Java Programming have a look on Statement and PreparedStatement