首先,你有一个问题与c = db.query("rates_table", columns, "place1", null, null, null, null);
的第三个参数将导致没有行被选中。您可以使用c = db.query("rates_table", columns, null, null, null, null, null);
,这将返回所有行。
或者您可以使用c = db.query("rates_table", columns, "place1 = 'myplace'", null, null, null, null);
,在这种情况下,只会显示列place1中myplace值的行。
最佳实践方法是将第3个和第4个参数结合使用?占位符在第三PARM(如“PLACE1 =?”),并在第4个参数对应的ARGS(例如,新的String [] {“MyPlace酒店”}),所以复制你可以有前面的查询c = db.query("rates_table", columns, "place1=?", new String[]{"myplace}, null, null, null);
使用c.moveToNext
,将尝试移动到光标的下一行(最初是第一行)。但是,如果它不能移动(即没有行,就像上面描述的那样),它不会失败,而是返回false(如果光标可以移动,则返回true)。
所以你需要检查,否则,在没有行的情况下,尝试访问一个行将失败,光标出界索引0请求,大小为0(即你请求的第一个索引0 )当光标(行数)的大小为0
有不同的方法来检查。
但是我怀疑,那么你会想知道为什么你的循环只显示1列。这将是因为你在查询中所说的只得到1列。
如果更改了查询的第二个参数为null,它会得到所有列。
在猜测你想返回所有地方的数组。
假设这则: -
// get Cursor with all rows(3rd parm null) for the place1 column (2nd parm)
c = db.query("rates_table", columns, null, null, null, null, null);
// Create String array according to the number of rows returned.
String[] places = new String[c.getCount()];
// loop through all rows setting the respective places element with the
// value obtained from the Cursor
while (c.moveToNext) {
places[c.getPosition()] = csr.getString(csr.getColumnIndex("place1"));
}
csr.close(); // Should always close a Cursor
return places;
这看起来错在一大堆的水平。检查你的查询。你的where子句只是“place1”,根本没有任何意义。你的循环没有意义,因为它没有移动光标位置以及 –
谢谢,但我只是找到了我正在寻找的答案。看到我的答案。 –