How to retrieve data based on two conditions given?

柔情痞子 提交于 2019-12-24 15:10:01

问题


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

 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

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 ?

Edited

 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;
    }

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


回答1:


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);

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) });

And since you are querying on a date format with zero padded numbers, use String.format("%02d",m) instead of String.valueOf(m).



来源:https://stackoverflow.com/questions/34371558/how-to-retrieve-data-based-on-two-conditions-given

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!