我正在尝试使用 Java 导入从我的数据库中读取 java.sql.PreparedStatement
和java.sql.ResultSet
。我的数据库有我试图查询的列,但它给了我错误,所以我不知道如何修复这个错误。
Database error - can't select data from 'Transactions' table: no such column: 'Date'
An error occurred! Database error - can't select data from 'Transactions' table: no such column: 'Date'
我尝试使用的 SQL 语句是 SELECT substr(Date, 4, 2), SUM(Price) FROM Transactions GROUP BY substr(Date, 4, 2)
。该语句在 SQLite Studio 中运行时工作正常。我尝试在 Java 中使用该语句,而不使用 substr
函数并且它运行时没有错误,但它没有提供所需的结果。
public static String selectAllMonth(List<Transaction> targetList) {
targetList.clear();
try {
PreparedStatement statement2 = DatabaseConnection.newStatement(
"SELECT substr(Date, 4, 2), SUM(Price) FROM Transactions GROUP BY substr(Date, 4, 2)"
);
if (statement2 != null) {
ResultSet results2 = statement2.executeQuery();
if (results2 != null) {
while (results2.next()) {
targetList.add(new Transaction(0,null,results2.getString("Date"), results2.getDouble("Price"),0,null));
}
}
}
} catch (SQLException resultsException) {
String error = "Database error - can't select data from 'Transactions' table: " + resultsException.getMessage();
Logger.log(error);
return error;
}
return "OK";
}
最佳答案
尝试 SELECT substr(Date, 4, 2) as 'Date', SUM(Price) as 'Price' FROM Transactions GROUP BY substr(Date, 4, 2)
关于java - 数据库错误 - 无法从 'Transactions' 表 : no such column: 'Date' - Java Error 选择数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54493407/