SQLite的动态查询 [英] SQLite dynamic query

查看:388
本文介绍了SQLite的动态查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SQLite数据库,和我想要做的是,一个用户选择的过滤器。比如我有一个图书数据库,用户只是想从阿加塔克里斯蒂书看的数据。

I have a SQLite database, and what i want to do is that an user selects a filter. For example i have a database of books, and a user just want to look data from "Agata christies books"..

所以我做的选项微调来选择,然后用我的意图传递选好了执行查询选择另一个活动现场。

So i made a spinner with the options to select, and then with an intent i pass the field selected to another activity which executes the query select.

我的问题是,如何才能让动态查询?认为我有超过1过滤器,我怎样才能使WHERE根据我从其他活动所传递的意图数据的条款?

My question is, how can i make the dynamic query? Think that i have more than 1 filter, how can i make the clause WHERE depending on the data i passed from the other activity by an intent?

感谢

推荐答案

偷懒的办法,但尝试和真正的:

Lazy way but tried and true:

String query = "Select id FROM books WHERE 1=1"
if (condition1) query+= " AND name="+theName;
if (condition2) query+= " AND author="+theAuthor;
if (condition3) query+= " AND price="+thePrice;

如果您通过又名纺纱的选择完全控制,这是安全的。如果一个EditText,使用preparedStatements和绑定的参数,以避免SQLI。

If you have full control of options aka via spinners, this is safe. If its an edittext, use preparedStatements and bind the arguments to avoid SQLI.

这篇关于SQLite的动态查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆