SQLite 查询:使用 BETWEEN 子句选择查询

发布于 2024-09-25 06:06:57 字数 765 浏览 3 评论 0原文

我想在 Android 应用程序中运行此查询:

SELECT field1 FROM table1  
where field1 = ?(enteredField1) AND field2 = ?(enteredField2)  
AND enteredField3 BETWEEN field3 , field4  
;

我的数据库架构

table1(field1, field2, field3, field4, field5)  

EnteredField1 , 2 , 3 = 是要检查的参数。

我希望我的问题很清楚。

这是函数 -

public boolean checkData(String task1, String dte1, String startTime1 )  
{   

        // field 1 = task , fiels 2 = dte, field 3 = startHr, field 4 = endHr  
        // enteredField 1,2,3 = task1, dte1, startTime1   
        // here I want to insert the query  

    int a = cursor.getCount();  
    if(a>0)  
    {  
        return true;  
    }  
    return false;  
}  

I want to run this query in Android Application :

SELECT field1 FROM table1  
where field1 = ?(enteredField1) AND field2 = ?(enteredField2)  
AND enteredField3 BETWEEN field3 , field4  
;

my DB schema

table1(field1, field2, field3, field4, field5)  

enteredField1 , 2 , 3 = are parameters which is to be checked.

I hope I am clear with my question.

here is the function -

public boolean checkData(String task1, String dte1, String startTime1 )  
{   

        // field 1 = task , fiels 2 = dte, field 3 = startHr, field 4 = endHr  
        // enteredField 1,2,3 = task1, dte1, startTime1   
        // here I want to insert the query  

    int a = cursor.getCount();  
    if(a>0)  
    {  
        return true;  
    }  
    return false;  
}  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陪你到最终 2024-10-02 06:06:57

首先,您的查询无效。 SQL 中没有 INBETWEEN; use BETWEEN

... test_expr BETWEEN lower_bound_expr AND upper_bound_expr

此外,逗号不是在 WHERE 子句中使用的二元运算符;使用 AND 代替:使用

... WHERE binary_expression_1 AND binary_expression_2

SQLiteDatabase 中的查询方法。使用哪种取决于您的需要;我链接的那个可能适合你的。

First, your query is invalid. There is no INBETWEEN in SQL; use BETWEEN:

... test_expr BETWEEN lower_bound_expr AND upper_bound_expr

Also, the comma is not a binary operator that works in WHERE clauses; use AND instead:

... WHERE binary_expression_1 AND binary_expression_2

Execute the query with one of the query methods in SQLiteDatabase. Which you use depends on your needs; the one I linked to is probably fine for yours.

小巷里的女流氓 2024-10-02 06:06:57

您可以使用 SQLiteQueryBuilder 来解决您的问题。下面小编给大家带来一个例子,希望对大家有所帮助。

SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
SQLiteDatabase db = null; // get your database instance here.
qb.setTables("table1");
qb.appendWhere("field1 = ? AND field2 = ?");
qb.query(db, new String[] { "field1" }, "enteredField3 BETWEEN field3 AND field4",
        new String[] { "enteredField1", "enteredField2"}, null, null, null);

you can use SQLiteQueryBuilder to solve your problem. Following is an example for you, hope it can help.

SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
SQLiteDatabase db = null; // get your database instance here.
qb.setTables("table1");
qb.appendWhere("field1 = ? AND field2 = ?");
qb.query(db, new String[] { "field1" }, "enteredField3 BETWEEN field3 AND field4",
        new String[] { "enteredField1", "enteredField2"}, null, null, null);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文