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

Posted

技术标签:

【中文标题】查询从 sqlite 数据库中检索两个时间之间的数据?【英文标题】:Query for retrieving data between two timings from sqlite databse? 【发布时间】:2017-04-12 02:16:32 【问题描述】:

如何根据当前时序从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 >= time('now', 'localtime')\n" + "" + "AND ToTiming 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】:
select * from TableName where FromTime between '2016-10-14 10:15:40.127' and '2016-11-05 12:44:17.553'

【讨论】:

以上是关于查询从 sqlite 数据库中检索两个时间之间的数据?的主要内容,如果未能解决你的问题,请参考以下文章

SQL查询从两列中检索两个日期之间的数据

DataGrip 和 SQLite - 从数据库中检索信息

sqlite 和数据库工作的新手,我该如何做这个查询?

sqlite3 中用于按月检索数据的查询是啥

在具有多个表的 sqlite 数据库中检索链接对象列表的查询是啥?

Mysql查询使用两个日期之间检索记录