所以我试图让一个基本的sql字符串工作,它将根据日期之间获取sqlite数据库中的记录。但是,出于某种原因,它不起作用。我只是不明白为什么。
private void viewTransactionsBetweenDatesTable(){
//Sets the table to view transactions between certain dates
try{
//Get's the dates from startDateChooserTransactions1 and endDateChooserTransactions1
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
DateFormat df2 = new SimpleDateFormat("MMM dd, yyyy");
Date sdct = startDateChooserTransactions1.getDate();
Date edct = endDateChooserTransactions1.getDate();
String sdcts = df.format(sdct);
String edcts = df.format(edct);
String sdctlabel = df2.format(sdct);
String edctlabel = df2.format(edct);
//Child's ID
String cid = childIDCheck1.getText();
//Grab's the specified data and places that as the table
String sql = "SELECT * FROM ChildrenPayment WHERE ChildID='"+cid+"' AND strftime('%Y-%m-%d', 'Report Transaction Date') BETWEEN '"+sdcts+"' AND '"+edcts+"' ";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
//Sets up the table
Info1.setModel(DbUtils.resultSetToTableModel(rs));
TableColumnModel tcm = Info1.getColumnModel();
//tcm.removeColumn(tcm.getColumn(3));
// tcm.removeColumn(tcm.getColumn(3));
// tcm.removeColumn(tcm.getColumn(10));
// tcm.moveColumn(11, 10);
// tcm.removeColum(tcm.getColumn(13));
//Changes modLabel1
modLabel1.setText(firstNameEditClass1.getText() + " " + lastNameEditClass1.getText() + " Between " + sdctlabel + " - " + edctlabel);
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}finally{
try{
pst.close();
rs.close();
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
}
我使用的是jdatechoos,因此与更好的DateTimeFormatter相比,我有点被迫使用SimpleDateFormat。无论如何,我根据YYYY-MM-DD格式化它,就像sqlite喜欢的那样,但是当我运行该函数时,表格不会显示任何内容。我设置了相当广泛的日子(2018年2月1日到2018年2月14日),我的数据库中的日期是2018年2月7日。我在数据库中有一些记录可以提取。但是,它并没有这样做。并且没有出现错误,因此我不知道它为什么不起作用。
Image of the records that I'm trying to place into my jtable
编辑1:在我忘记之前,我还尝试了以下SQL字符串
String sql = "SELECT * FROM ChildrenPayment WHERE ChildID='"+cid+"' AND 'Report Transaction Date' BETWEEN '"+sdcts+"' AND '"+edcts+"' ";
答案 0 :(得分:0)
这不起作用:
strftime('%Y-%m-%d', 'Report Transaction Date')
因为您提供的格式说明符要求您提供三个值,分别为年,月和日各一个。
如果数据库中的日期存储为完整的SQLite日期时间字符串,则必须使用
"... date([Report Transaction Date]) BETWEEN '"+sdcts+"' AND '"+edcts+"' ";
注意列名称周围的方括号(不是单引号)。这与需要日期/时间值无关,这是因为列名中包含空格。任何带空格的列名都必须用双引号或方括号括起来。这就是为什么永远不要在列名中使用空格的原因。
如果它们实际上存储为“YYYY-MM-DD”字符串,那么您的备选方案无法工作的原因是因为您单引引了列名'Report Transaction Date'
,这导致比较文字字符串到日期值。