SQLite 中关于日期的查询处理

sqlite 中可以根据日期来筛选数据。

日期格式 经过测试支持yyyy-mm-dd和yyyy-mm-dd HH:mm:ss,。

但是 在测试时出现了一个问题:

当我用?代替参数时:

Cursor cursor = dbHelper.getReadableDatabase().rawQuery("select * from test where h_time>='?' and h_time<='?'",new String[]{"1991-07-01 00:00:00","1991-07-01 23:59:59"})

Logcat出现如下错误


直接用sql语句:
Cursor cursor = dbHelper.getReadableDatabase().rawQuery("select h_name from test where h_time>='1991-07-03 00:00:00' and h_time<='1991-07-03 23:59:59'",null);
程序运行正常,能查询出预期结果。

然后又尝试了几种写法:单引号加到String[]里面:
Cursor cursor = dbHelper.getReadableDatabase().rawQuery("select h_name from test where h_time>=? and h_time<=?",new String[]{"'1991-07-01 00:00:00'","'1991-07-01 23:59:59'"});
与不加单引号
Cursor cursor = dbHelper.getReadableDatabase().rawQuery("select h_name from test where h_time>=? and h_time<=?","1991-07-01 23:59:59"});
程序没有报错,但是没有出现预期的查询结果。

以后关于时间的判断处理,尽量在sql语句中通过拼接字符串完成。

出现上述情况。希望有知道的人解答。


测试代码下载

相关文章

SQLite架构简单,又有Json计算能力,有时会承担Json文件/RES...
使用Python操作内置数据库SQLite以及MySQL数据库。
破解微信数据库密码,用python导出微信聊天记录
(Unity)SQLite 是一个软件库,实现了自给自足的、无服务器...
安卓开发,利用SQLite实现登陆注册功能