【问题标题】:SQLite / Selecting rows from table WHERE column = ANYTHINGSQLite / 从表 WHERE 列中选择行 = ANYTHING
【发布时间】:2018-10-09 07:34:55
【问题描述】:

在 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 variable = 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,只要它是任何给定的变量,但只要它们已经是同一列的任何给定的两个足够不同的值,它也会这样做吗?

【问题讨论】:

  • 您想选择 status_type 不为空的位置是否正确?
  • 是的,只要status_type 变量也不为空,您就可以回答如何操作。在我的真实案例中,status_type 变量可以从 3 个不同值中的 1 个中取值,-1 表示待处理片段,1 表示计划片段,3 表示已完成片段。对于所有片段,status_type 可以是我上面提到的 3 个不同值中的任何值。
  • 我也可以做到"SELECT COUNT(Loan_No) FROM " + TABLE_AGEING_VALUES_ALL + " WHERE user_id = \"" + user_id + "\" AND Loan_No = \"" + Loan_No + "\" AND (status_type = -1 OR status_type = 1 OR status_type = 3)"。但是,当status_type 的所有可能值有很多不同的选择或者它们有无限多个时,情况如何?
  • 您实际上可以检查AND status_type NOT NULL

标签: android sql sqlite select android-sqlite


【解决方案1】:

如果您正在寻找一种方法来使用相同的 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();

【讨论】:

  • 确实,如果 "status_type = status_type" 由于某种原因根本无法运行,与 "1 = 1" 不同,那么使用 "1 = ? OR status_type = ?" 这样做是个好主意,类似于 SQLite 代码注射。它需要 2 个参数,而不是 1 个参数,但只要我们想要 status_type 变量 = 某个特定值,我们就可以像下面这样传递,例如:"1 = 0 OR status_type = ?",为status_type 变量提供一些特定的给定值:?.
  • 另一方面,只要我们想要 status_type 变量 = 任何东西,我们就可以通过如下方式传递,例如:"1 = 1 OR status_type = ?",具有任何给定值对于status_type 变量:?.
  • 顺便说一下,我没有在 SQLite 查询上使用 INSERT 或 UPDATE 语句,以便能够使用 ps.setInt(1, 1); // user_id = 1ps.setInt(2, 0); // 0 means "specific status_type"ps.setInt(3, 2); // status_type = 2 这样做。或ps.setInt(1, 1); // user_id = 1ps.setInt(2, 1); // 1 means "any status_type"ps.setInt(3, 0); // (this parameter is ignored)。我正在将值传递给可能的 Java 函数中可用的不同变量,所以值?变量可以从 SQLite 查询中传递,只需在一行 SQLite 查询中使用即可。
  • 确实,如果"status_type = status_type""1 = 1" 不同,由于某种原因根本无法解决,那么它永远不会这样做吗?
【解决方案2】:

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

查询将是

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

【讨论】:

  • 我已经提到我从 Java 函数将值传递给 status_type 变量,所以这个解决方案根本行不通。
  • 通过使用不同的工具和技术。或 - 或通过使用相同的或 - 或然后不同的方式和方法 - 技术与工具。或者必须并且应该只在适当的游戏中发布给玩家的适当消息 - 玩游戏的动作 - 或每一个 - 只需要 - 足够 - 只需要 - 足够。或者然后通过使用相同然后不同或方式然后方法 - 或工具然后技术,或者......
  • 或者必须并且应该只在适当的游戏中发布给玩家的适当消息 - 玩游戏的动作 - 或每一个 - 只需要 - 足够 - 只需要 - 足够。那个曾经,那个曾经,一个方式,一个方式。
【解决方案3】:

也许考虑使用类似的东西:-

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'
  • 注意这是原则性代码,未经测试,因此可能包含小错误。

【讨论】:

  • 如果像这样让代码更复杂,有多个'if'子句的类型,那么它不会对移动应用程序的效率产生一种影响吗?是否没有任何其他类型的方法可以作为一种替代解决方案,只需一行 SQLite 查询即可完成您上面提到的全部事情?顺便说一句,对效率的影响类型只需要针对我的其他类型的问题以及此类问题进行衡量,而根本不需要针对此类问题。
  • 对效率的最大影响是设计而不是奇怪的 if 语句来弥补糟糕的设计,当然,这在任何 SQL 语句中都是必需的。
猜你喜欢
  • 2023-04-03
  • 1970-01-01
  • 2013-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-22
  • 2021-07-10
  • 2013-05-27
相关资源
最近更新 更多