gpt4 book ai didi

java - 如何计算包含特定值的行数

转载 作者:行者123 更新时间:2023-12-01 11:15:07 25 4
gpt4 key购买 nike

我编写了下面的代码来从sqlite数据库中检索一些记录,但我想做的是计算表中包含特定(lat,lng)的所有rosw

sqliteFactory.getXMLPath("53.074415", "8.788047");

代码:

public void getNodeID(String lat, String lng) throws ClassNotFoundException, SQLException {
Connection conn = getConnection();
PreparedStatement ps = conn.prepareStatement("select "+this.NODE_ID_COL+" from "+this.TABLE_NAME+" where "+this.LAT_COL+" = ? And "+this.LNG_COL+" = ?");

ps.setString(1, lat);
ps.setString(2, lng);
ResultSet resSet = ps.executeQuery();

while (resSet.next()) {
Log.v(TAG, "getNodeID", "NodeID(lat="+lat+", lng="+lng+"): "+resSet.getString(this.NODE_ID_COL));
}

resSet.close();
ps.close();
conn.close();
}

最佳答案

不要从数据库中检索所有这些值,然后在 Java 中对它们进行计数(这可能涉及传输大量数据,并且可能非常慢/效率低下),而是要求数据库为您计算它们。

考虑这样的事情:

select count(*) from locations where latitude=53.074415 and longitude=8.788047;

关于java - 如何计算包含特定值的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31925532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com