查询从sqlite数据库检索两个时间之间的数据?

时间:2016-11-28 11:12:52

标签: android sqlite

如何根据当前时间从sqlite的两个时间之间检索数据。

我使用此查询但不起作用

 final Cursor cView = db.rawQuery("SELECT * FROM proname WHERE FromTiming >= time('now', 'localtime')\n" + "" +
            "AND ToTiming <= time('now', 'localtime')\n" + " ORDER BY FromTiming LIMIT 1", null);

我的oncreate是:

public static final String[] titles = new String[]{"Akilam 360","Ipadikku Idhayam",
        "Palsuvai Thoranam"};



public static  final String[] fromtime = new String[]{"5:00:00","07:00:00","09:00:00"};

public static  final String[] totime = new String[]{"6:59:59","08:59:59","10:59:59"};


public static final Integer[] images = {R.drawable.akilam_360,
        R.drawable.ipadikku_idhayam, R.drawable.palsuvai_thoranam};

ListView listView;
List<Program> rowItems;
int  iImageId;
String sTitle,sFrom,sTo ;
SQLiteDatabase db;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dbcon);



    Calendar c = Calendar.getInstance();

    int seconds = c.get(Calendar.SECOND);
    int minutes = c.get(Calendar.MINUTE);
    int hour = c.get(Calendar.HOUR_OF_DAY);
    String time = hour+":"+minutes+":"+seconds;

    timer = (TextView) findViewById(R.id.timer);
    timer.setText(time);




    db =openOrCreateDatabase("MukilProgram", Context.MODE_PRIVATE,null);
    db.execSQL("CREATE TABLE IF NOT EXISTS proname(ImageID INTEGER,Title TEXT,FromTiming INTEGER,ToTiming INTEGER);");

    rowItems = new ArrayList<Program>();
    for (int i = 0; i < titles.length; i++) {
        Program item = new Program(images[i], titles[i],fromtime[i],totime[i]);
        rowItems.add(item);
    }

我的检索部分:

final Cursor cView = db.rawQuery(“SELECT * FROM proname WHERE FromTiming&gt; = time('now','localtime')\ n”+“”+                 “AND ToTiming&lt; = time('now','localtime')\ n”+“ORDER BY FromTiming LIMIT 1”,null);         if(cView.getCount()&gt; 0){

        sImageID = new ArrayList<String>();
        sName = new ArrayList<String>();
        iFrom = new ArrayList<String>();
        iTo = new ArrayList<String>();



        while (cView.moveToNext()) {

            sImageID.add(cView.getString(0));
            sName.add(cView.getString(1));
            iFrom.add(cView.getString(2));
            iTo.add(cView.getString(3));

            //     sQuantity.add(cView.getString(2));
            SQliteAdapter sqliteadapter = new SQliteAdapter(Databaseconnection.this, sImageID, sName,iFrom,iTo);
            listView.setAdapter(sqliteadapter);
        }

    }

此查询有效,但我没有收到任何结果或错误,请帮我写正确的查询。

1 个答案:

答案 0 :(得分:0)

select * from TableName where FromTime between '2016-10-14 10:15:40.127' and '2016-11-05 12:44:17.553'