gpt4 book ai didi

java - 如果我只使用 SQLite 返回一行,是否需要循环? (安卓开发)

转载 作者:行者123 更新时间:2023-12-01 17:34:00 25 4
gpt4 key购买 nike

我一直在使用以下方法使用 for 循环,但我突然想到我只返回一行 - 那么是否有必要有一个循环?

即我的代码类似于:

public String topSwimmerSponsor() {
Cursor c = ourDatabase.rawQuery("SELECT " + KEY_SWIMMERLAPS + " * "
+ KEY_SPONSOR + " AS result, " + KEY_NAME + " FROM "
+ DATABASE_TABLE + " ORDER BY result DESC limit 1", null);

String result = "";

for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
result = result + c.getString(1) + "\n";
}
return result;
}

但是我试图将其更改为以下内容:

public String topSwimmerSponsor() {
Cursor c = ourDatabase.rawQuery("SELECT " + KEY_SWIMMERLAPS + " * "
+ KEY_SPONSOR + " AS result, " + KEY_NAME + " FROM "
+ DATABASE_TABLE + " ORDER BY result DESC limit 1", null);

String result = "";

result = result + c.getString(1) + "\n";

return result;
}

但是我的应用程序崩溃了。实现这个的正确方法是什么?谢谢。

最佳答案

如果您只期望一个结果,您可以使用此:

if(c.moveToFirst())
return c.getString(1) + "\n";

关于java - 如果我只使用 SQLite 返回一行,是否需要循环? (安卓开发),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8194792/

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