给如何检索基于两个条件的数据? [英] How to retrieve data based on two conditions given?

查看:189
本文介绍了给如何检索基于两个条件的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MainActivity,我想在的ListView 与特定的月份显示的数据。

In MainActivity, I want the listView display the data with a specific month.

 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糊涂一点点。什么是为了获取基于两个条件的数据写入正确的方法是什么?

I get a little bit confused in rawQuery. What is the correct way to write in order to retrieve the data based on two conditions ?

编辑

 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没有数据显示。

For the Date column, it holds the date format 19-12-2015. No error now but no data display on ListView.

推荐答案

有没有rawQuery方法,它有两个独立的的String [] 。作为由误​​差指出

There is no rawQuery method that takes two separate String[]. As pointed out by the error

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

而不是查询像这样的

Instead of querying like so

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

使用此相反,在您的WHERE语句列在同一个的String []

Use this instead, where your columns for the WHERE statement are in the same String[]

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

甚至干脆是因为你不需要空结尾

Or even simply this because you don't need the null at the end

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

既然你是一个日期格式补零号查询,使用的String.format(%02D,M)而不是将String.valueOf(M)

这篇关于给如何检索基于两个条件的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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