SQLite /从表WHERE列= ANYTHING中选择行

时间:2018-04-28 18:10:19

标签: android sql sqlite select android-sqlite

在Android SQLite中,我有一个表:TABLE_AGEING_VALUES_ALL

Loan_No text primary key,
agreement_date date,
branch_name text,
loan_status text,
address1 text,
address2 text,
status_type integer,
user_id integer

并考虑以下SQLite查询:

"SELECT COUNT(Loan_No) FROM " + TABLE_AGEING_VALUES_ALL + " WHERE user_id = \"" + user_id + "\" AND Loan_No = \"" + Loan_No + "\" AND status_type = " + status_type

当我想要status_type变量= ANYTHING时,我可以简单地调用:

"SELECT COUNT(Loan_No) FROM " + TABLE_AGEING_VALUES_ALL + " WHERE user_id = \"" + user_id + "\" AND Loan_No = \"" + Loan_No + "\""

但是因为我从Java函数调用中将值传递给status_type变量,所以我不能简单地删除该列。

那么,当我想要status_type变量= ANYTHING时,从Java函数调用将值传递给status_type变量的最有效方法是什么?

例如,

"SELECT COUNT(Loan_No) FROM " + TABLE_AGEING_VALUES_ALL + " WHERE user_id = \"" + user_id + "\" AND Loan_No = \"" + Loan_No + "\" AND status_type = *"

根本没用。

我已经在Stack Overflow中搜索了它最相关的解决方案,已经足够了:

我有类似的东西,比如:

"SELECT COUNT(Loan_No) FROM " + TABLE_AGEING_VALUES_ALL + " WHERE user_id = \"" + user_id + "\" AND Loan_No = \"" + Loan_No + "\" AND status_type = status_type"

但是,为了解决这个问题,是否有足够的解决方案来解决这个问题?

我同意"status_type = status_type"只要它是任何给定的变量就会一直返回1,但是只要它们已经任意给定两个不同的相同列的值,它也会同样返回def post_create(request): ImageFormSet = modelformset_factory(Images, fields=('image','image_title', 'image_description'), extra=7) if request.method == "POST": form = PostCreateForm(request.POST or None) formset = ImageFormSet(request.POST or None, request.FILES or None) if form.is_valid() and formset.is_valid(): instance = form.save(commit=False) instance.author = request.user instance.save() print(formset.cleaned_data) for f in formset.cleaned_data: try: photo = Images(post=instance, image=f['image'], image_title=f['image_title'], image_description=f['image_description']) photo.save() except Exception as e: break return redirect('posts:single', username=instance.user.username, slug=instance.slug) else: form = PostCreateForm() formset = ImageFormSet(queryset=Images.objects.none()) context = { 'form': form, 'formset': formset, } return render(request, 'blog/post_create.html', context)

3 个答案:

答案 0 :(得分:2)

如果您想要获取所有status_type,则无需在查询中指定它。

查询将是

" SELECT COUNT(Loan_No)FROM" + TABLE_AGEING_VALUES_ALL +" WHERE user_id = \"" + user_id +" \" AND Loan_No = \"" + Loan_No

答案 1 :(得分:2)

或许考虑使用以下方面的内容: -

public String buildGetLoadCountSQL(Integer userid, String loan_no, Integer status_type) {

    String whereclause = "";
    if(userid != null) {
        whereclause = whereclause + " user_id=" + String.valueOf(userid);
    }
    if (loan_no != null && loan_no.length() > 0) {
        if (whereclause.length() > 0) {
            whereclause = whereclause + " AND ";
        }
        whereclause = whereclause + " Loan_No=?'" + loan_no + "' ";
    }
    if (status_type != null) {
        if (whereclause.length() > 0) {
            whereclause = whereclause + " AND ";
        }
        whereclause = whereclause + " status_type=" + String.valueOf(status_type);
    }
    if (whereclause.length() > 1) {
        whereclause = " WHERE " + whereclause;
    }
    return "SELECT COUNT(Loan_No) FROM " + TABLE_AGEING_VALUES_ALL + whereclause;
}

使用此功能,您可以通过在其中指定null来省略任何值。

例如

    buildGetLoadCountSQL(null,null,null);

返回(所有行): -

    SELECT COUNT(Loan_No) FROM your_table

    buildGetLoadCountSQL(10,"0123456789",10);

返回(最具选择性): -

    SELECT COUNT(Loan_No) FROM your_table WHERE userid=10 AND Loan_No= '0123456789' AND status_type=10

    buildGetLoadCountSQL(10,"0123456789",null);

返回(您想要提出的问题): -

    SELECT COUNT(Loan_No) FROM your_table WHERE userid=10 AND Loan_No= '0123456789'
  • 注意这是原则上的代码,尚未经过测试,因此可能包含轻微错误。

答案 2 :(得分:2)

如果您正在寻找使用相同SQL命令文本执行两种类型查询的方法,那么您可以使用如下查询:

String sql = 
          "SELECT COUNT(Loan_No) FROM TABLE_AGEING_VALUES_ALL "
        + "WHERE user_id = ? "
        + "    AND (1 = ? OR status_type = ?)";
PreparedStatement ps = conn.prepareStatement(sql);

然后,要匹配特定的status_type,您可以使用

// query for user_id = 1 and a specific status_type
ps.setInt(1, 1);  // user_id = 1
ps.setInt(2, 0);  // 0 means "specific status_type"
ps.setInt(3, 2);  // status_type = 2
ResultSet rs = ps.executeQuery();

对于匹配任何status_type值的查询,您可以使用

// query for user_id = 1 and any status_type
ps.setInt(1, 1);  // user_id = 1
ps.setInt(2, 1);  // 1 means "any status_type"
ps.setInt(3, 0);  // (this parameter is ignored)
ResultSet rs = ps.executeQuery();