【问题标题】:How to retrieve data based on two conditions given?如何根据给定的两个条件检索数据?
【发布时间】:2015-12-19 13:55:49
【问题描述】:

在 MainActivity 中,我希望 listView 显示特定月份的数据。

 public void BuildList()
    {
        sqlcon.open(); // object of InfoAPI
        Calendar cal = Calendar.getInstance();
        int month = cal.get(Calendar.MONTH)+1;
        Toast.makeText(getApplicationContext(),month+"",Toast.LENGTH_LONG).show();
        Cursor cursor1=sqlcon.readData(month);
        String[] columns=new String[]{
        MyDatabaseHelper.Date,MyDatabaseHelper.TimeIn_Info,MyDatabaseHelper.TimeOut_Info
        };

        int[] to=new int[]
         {
           R.id.date,R.id.timeIn,R.id.timeOut
         };

        dataAdapter = new SimpleCursorAdapter(this, R.layout.retrieve_data,
                cursor1,
                columns,
                to,
                0);

        dataAdapter.setViewBinder(new ViewBinder());
        listView.setAdapter(dataAdapter);
    }

}

InfoAPI

public Cursor readData(int m)
    {
        Cursor c=database.rawQuery(" SELECT _id, Date,Weather,Status, TimeIn_Info, TimeOut_Info FROM " + MyDatabaseHelper.TABLE_INFO + 
                " WHERE Name = ? and strftime('%m',Date)= ?", 
                new String[]{"LCV"}, new String[]{String.valueOf(m)},null);

        if (c != null) {
            c.moveToFirst();
        }
        return c;
    }

错误

 Error:(54, 26) error: no suitable method found for rawQuery(String,String[],String[],<null>)
    method SQLiteDatabase.rawQuery(String,String[]) is not applicable
    (actual and formal argument lists differ in length)
    method SQLiteDatabase.rawQuery(String,String[],CancellationSignal) is not applicable
    (actual and formal argument lists differ in length)

Error:(106, 30) error: method readData in class InfoAPI cannot be applied to given types;
required: int
found: no arguments
reason: actual and formal argument lists differ in length

我对 rawQuery 有点困惑。为了根据两个条件检索数据,正确的编写方法是什么?

已编辑

 public Cursor readData(int m)
    {
        Cursor c=database.rawQuery(" SELECT _id, Date,Weather,Status, TimeIn_Info, TimeOut_Info FROM " + MyDatabaseHelper.TABLE_INFO +
                        " WHERE Name = ? and strftime('%m',Date)= ?",
                new String[]{ "LCV", String.valueOf(m) }, null);
        if (c != null) {
            c.moveToFirst();
        }
        return c;
    }

对于日期列,它保存日期格式19-12-2015。现在没有错误,但 ListView 上没有数据显示。

【问题讨论】:

  • 有错误吗?我认为您需要将参数放在单个 String[] 中的查询中
  • 是的,请参阅,“找不到合适的方法”。这对你意味着什么?
  • @cricket_007 是因为strftime 吗?
  • 不,看documentation for rawQuery。有多少个参数?它们是什么类型的?这与您所拥有的有何不同?
  • 我试图帮助您学习阅读文档。您的错误很明显-您没有正确调用该方法。因此,您应该首先查看 Android 文档,了解调用该方法的正确方法。

标签: android sqlite listview where-clause


【解决方案1】:

没有采用两个单独的String[] 的 rawQuery 方法。正如错误所指出的那样

no suitable method found for rawQuery(String,String[],String[],<null>)

而不是像这样查询

rawQuery(query, new String[]{"LCV"}, new String[]{String.valueOf(m)},null);

改用这个,你的 WHERE 语句列在同一个 String[]

rawQuery(query, new String[]{ "LCV", String.valueOf(m) }, null);

或者甚至只是因为你最后不需要 null

rawQuery(query, new String[]{ "LCV", String.valueOf(m) });

由于您查询的是填充数字为零的日期格式,因此请使用String.format("%02d",m) 而不是String.valueOf(m)

【讨论】:

  • 谢谢你的帮助兄弟..但我不知道为什么listView上没有显示数据
  • 欢迎。我将无法提供帮助,因为要检查的部分太多。例如...您确定您查询的月份的数据存在吗?您确定您的查询是正确的并且实际返回结果吗?您确定正确使用 SimpleCursorAdapter 吗?
  • 在我添加 month 参数之前,我只使用名称(LCV),数据得到显示
  • 带有 %m 的 strftime 是一个零填充月份。如果您正在搜索小于 10 的月份,那么它将返回零结果,因为 String.valueOf(m) 将是一位数。
  • 我不知道还有什么可以告诉你的,除了验证实际上有一行以 LCV 作为名称和一个带有相应月份的日期。如果您仍然无法弄清楚,您应该发布一个新问题,因为您已经在这里接受了答案。
猜你喜欢
  • 2012-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-16
  • 1970-01-01
  • 1970-01-01
  • 2020-10-13
  • 2020-12-21
相关资源
最近更新 更多