Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
276 views
in Technique[技术] by (71.8m points)

SQLite Database in Android for select query along with between clause

I am using SQLite Database in Android. I have used rawQuery in SQLite database. My Database method for that is as follows:

public Cursor selectedDate(String from_dt ,String to_dt) 
{
    try
    {
        String str_k="select RowId,Bill_Date from  SalesMaster Where Bill_Date between '"+
                         from_dt+ "'and'"+to_dt+ "'";
        System.out.println(str_k);

        Cursor c1 = sqldb.rawQuery(
                        "select RowId from  SalesMaster Where Bill_Date between '"+
                        from_dt+ "'and'"+to_dt+ "'", null);
        return c1; 
    }
    catch(Exception e)
    { 
        System.out.println("inside database file "+ e);
        return null;
    }
}

and my .java file is

public void ReportSell(String from_dt,String to_dt)
{
    System.out.println("report selling method has been called");
    String RowId="";
    item db1 = new item(getBaseContext());
    db1.openDb();

    try
    {
        final Cursor cursor = db1.selectedDate(from_dt, to_dt);

        if(cursor.moveToFirst())
        {
            do
            {
                RowId= cursor.getString(cursor.getColumnIndex("RowId")).toString();
                System.out.println("inside java Row Id  file "+ RowId);
                salesid.add(RowId);
                ArrayAdapter <String> adapter = new ArrayAdapter <String>(this, 
                                                     android.R.layout.simple_list_item_1,
                                                     salesid);
            } while(cursor.moveToNext());
        }
        else
        {
            Toast.makeText(getApplicationContext(), "No data found",
                               Toast.LENGTH_LONG).show();  
        }
    } catch(Exception e) 
    {
        System.out.println("inside java file "+ e);
    }
    db1.closeDb(); 
}

The main problem is that it will return RowId from salesMaster table as per from-date and to-date, but actual problem is it will return values but not include last to-date.

For example, if I want to RowId from 25/02/2015 to 28/02/2015 so it will give RowId from 25/02/2015 to 27/02/2015 but not included 28/02/2015.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have to replace between to

Expression (min <= expr AND expr <= max)

Change you query to

 String str_k="SELECT * from TABLE_NAME Where COLUMN_NAME >='"+from_date+ "' AND <= '"+to_date+ "'";

            System.out.println(str_k);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...