在sqlite中按日期时间选择行

时间:2016-02-18 12:10:43

标签: c# sqlite

我正在尝试使用sqlite在c#中选择startdate和enddate之间的行。

我的日期格式如下:

2015-05-16T17:22:04.920+02:00

应该是sqlite的有效格式(ref:http://www.sqlite.org/lang_datefunc.html

我正在尝试选择这样的行:

string CmdString = "SELECT * FROM " + tablename +  "WHERE DATETIME(timekey) >= DATETIME('2015-05-16T17:22:04.920+02:00') AND DATETIME(timekey) <= DATETIME('2015-05-16T17:24:04.920+02:00')";
SQLiteCommand cmd = new SQLiteCommand(CmdString, con);
SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd);
DataTable MatchDt = new DataTable();

sda.Fill(MatchDt);

我得到的错误是:

SQLiteException: SQL logic error or missing database

我做错了什么?

1 个答案:

答案 0 :(得分:0)

您在where:

之前缺少空格字符
string CmdString = "SELECT * FROM " + tablename +  " WHERE DATETIME(timekey) >= DATETIME('2015-05-16T17:22:04.920+02:00') AND DATETIME(timekey) <= DATETIME('2015-05-16T17:24:04.920+02:00')";

我建议使用BETWEEN运算符:

string CmdString = "SELECT * FROM " + tablename +  " WHERE DATETIME(timekey) BETWEEN DATETIME('2015-05-16T17:22:04.920+02:00') AND DATETIME('2015-05-16T17:24:04.920+02:00')";