我正在尝试在我的Java程序中编写一个查询来选择两个日期之间的记录。我已经建立了我的日期:
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yy", Locale.ENGLISH);
Date parsed;
try
{
parsed = formatter.parse(sqlDateIn);
java.sql.Date sqlDate = new java.sql.Date(parsed.getTime());
returnDate = sqlDate;
}
当我查询数据库时,我得到附件。
我无法通过查询来选择任何记录(例如,在01/01/15和12/31/15之间)。我尝试了各种strftime
的组合,但无济于事。
我是Java和SQLite的菜鸟,所以我确信这是一个愚蠢的用户错误。
对不起我在下面的回复。
SELECT Course, date DateSort FROM Peter_SCORE_TBL WHERE DateSort BETWEEN '01/01/15' AND '12/31/15'
SELECT Course, date DateSort FROM Peter_SCORE_TBL WHERE DateSort BETWEEN '2015/01/01' AND '2015/12/31'
SELECT Course, date DateSort FROM Peter_SCORE_TBL WHERE DateSort BETWEEN '2015-01-01' AND '2015-12-31'
SELECT Course, date DateSort FROM Peter_SCORE_TBL strftime('%m/%d/%Y', DateSort) BETWEEN '01/01/15' AND '12/31/15'
以上是我在Firefox SQLite Manager扩展程序中尝试的内容。
编辑: 在上面,我将DateSort定义为DATETIME。我已更改为DATE和VARCHAR并运行上述查询,未选择任何内容。
我也尝试过:
SELECT * FROM Peter_SCORE_TBL WHERE date(DateSort) BETWEEN date('01/01/15') AND date('12/31/15')
SELECT * FROM Peter_SCORE_TBL WHERE date(DateSort) BETWEEN date('2015-01-01') AND date('2015-12-31')
都没有奏效。不知道还有什么可以尝试。
编辑:
我抓住了这一小段代码并对其进行了修改以进行测试。这是行不通的。我错过了什么?
public class SQLiteTest
{
public static void main(String[] args) throws Exception
{
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite:/Users/peterream/Desktop/Desktop Archive/Crashplan/sqlitetest.sqlite");
Statement stat = conn.createStatement();
stat.executeUpdate("drop table if exists people;");
stat.executeUpdate("create table people (name TEXT, occupation TEXT, DateSort INTEGER);");
PreparedStatement prep = conn.prepareStatement(
"insert into people values (?, ?, ?);");
prep.setString(1, "Gandhi");
prep.setString(2, "politics");
prep.setDate(3, convertSQLDate("2016-04-08"));
prep.addBatch();
prep.setString(1, "Turing");
prep.setString(2, "computers");
prep.setDate(3, convertSQLDate("2015-04-08"));
prep.addBatch();
prep.setString(1, "Wittgenstein");
prep.setString(2, "smartypants");
prep.setDate(3, convertSQLDate("2014-04-08"));
prep.addBatch();
conn.setAutoCommit(false);
prep.executeBatch();
conn.setAutoCommit(true);
ResultSet rs = stat.executeQuery("select * from people WHERE strftime('%Y-%m-%d', DateSort) BETWEEN '2015-01-01' AND '2015-12-31';");
while (rs.next()) {
System.out.println("name = " + rs.getString("name"));
System.out.println("job = " + rs.getString("occupation"));
System.out.println("DateSort = " + rs.getString("DateSort"));
}
rs.close();
conn.close();
}
/**
**
* This method converts input string date to sql date format
*
* @param String sqlDateIn (MM/dd/yy)
* @return java.sql.Date returnDate
*/
public static java.sql.Date convertSQLDate (String sqlDateIn)
{
java.sql.Date returnDate = null;
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
java.util.Date parsed;
try
{
parsed = formatter.parse(sqlDateIn);
java.sql.Date sqlDate = new java.sql.Date(parsed.getTime());
returnDate = sqlDate;
}
catch (ParseException e)
{
e.printStackTrace();
}
return returnDate;
}
}
我希望选择1行2015-04-08。