Android SQLite 预准备语句生命周期
与原始 SQL 语句相比,SQLite 准备好的语句似乎是执行查询的好方法。我只是想知道它们应该保留多久。
静态编译该语句是否安全,即仅编译一次并在许多查询中继续使用相同的语句? 如果这不是线程安全的,那么在本地线程上怎么办? 相反,如果您只使用一次准备好的语句,它们仍然是首选吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
准备语句的优点是:
随着执行计划被缓存,性能会更好。
这是一种针对 SQL 注入进行编码的好方法,因为它会转义输入
值。
当涉及到没有未绑定变量的语句时,数据库
可以自由地进行最大程度的优化。单独的查询将是
速度更快,但缺点是你需要做数据库
一直在编译,这比编译的好处更糟糕
更快的查询。
对于 Java 编程,请查看 声明和PreparedStatement
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