如果 SQLite 游标没有结果,应用程序会崩溃
App crashes if SQLite cursor has no results
我有一个使用游标 运行 SQlite 查询的应用程序。
public Cursor totaltrips(){
Cursor cursor = database.rawQuery("SELECT * AS TTotal FROM " + DatabaseHelper.TABLE_NAME, null);
return cursor;
}
结果存储到一个最多有 5 个值的 Arraylist 中。如果数据库中没有记录,应用程序就会崩溃。如果我有一个或多个数据库条目,它就可以正常工作。有谁知道在没有数据库条目时如何阻止它崩溃?
// get column value
if (Distance.moveToNext())
result = String.valueOf(Distance.getDouble(Distance.getColumnIndex("myTotal")));
tnmView.setText(result);
List<String> distancearray = new ArrayList<String>();
Cursor cursor = dbManager.totaldistance();
do{
distancearray.add(cursor.getString(1));
}while ((cursor.moveToNext()));
ttrips = cursor.getCount();
Log.i("Graph", "TTRIPS = " + ttrips);
// Be sure here to have at least the 5 desired elements into the list
while(distancearray.size() < 5){
distancearray.add("0");
}
应用崩溃并出现错误
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
在线
do{
distancearray.add(cursor.getString(1));
}while ((cursor.moveToNext()));
检查光标是否真的有结果,例如尝试这样的事情:
int numResults = cursor.getCount();
if (numResults > 0) {
do {
distancearray.add(cursor.getString(1));
} while ((cursor.moveToNext()));
}
替换
do{
distancearray.add(cursor.getString(1));
}while ((cursor.moveToNext()));
和
if (cursor != null) {
while (cursor.moveToNext()) {
distancearray.add(cursor.getString(1));
}
cursor.close();
}
使用后检查游标是否为空并且有多个value.Close游标。
if(cursor!=null&&cursor.getCount()>0){
cursor.moveToFirst();
while(cursor.hasNext()){
//do stuff here
}
cursor.close();
}
我有一个使用游标 运行 SQlite 查询的应用程序。
public Cursor totaltrips(){
Cursor cursor = database.rawQuery("SELECT * AS TTotal FROM " + DatabaseHelper.TABLE_NAME, null);
return cursor;
}
结果存储到一个最多有 5 个值的 Arraylist 中。如果数据库中没有记录,应用程序就会崩溃。如果我有一个或多个数据库条目,它就可以正常工作。有谁知道在没有数据库条目时如何阻止它崩溃?
// get column value
if (Distance.moveToNext())
result = String.valueOf(Distance.getDouble(Distance.getColumnIndex("myTotal")));
tnmView.setText(result);
List<String> distancearray = new ArrayList<String>();
Cursor cursor = dbManager.totaldistance();
do{
distancearray.add(cursor.getString(1));
}while ((cursor.moveToNext()));
ttrips = cursor.getCount();
Log.i("Graph", "TTRIPS = " + ttrips);
// Be sure here to have at least the 5 desired elements into the list
while(distancearray.size() < 5){
distancearray.add("0");
}
应用崩溃并出现错误
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
在线
do{
distancearray.add(cursor.getString(1));
}while ((cursor.moveToNext()));
检查光标是否真的有结果,例如尝试这样的事情:
int numResults = cursor.getCount();
if (numResults > 0) {
do {
distancearray.add(cursor.getString(1));
} while ((cursor.moveToNext()));
}
替换
do{
distancearray.add(cursor.getString(1));
}while ((cursor.moveToNext()));
和
if (cursor != null) {
while (cursor.moveToNext()) {
distancearray.add(cursor.getString(1));
}
cursor.close();
}
使用后检查游标是否为空并且有多个value.Close游标。
if(cursor!=null&&cursor.getCount()>0){
cursor.moveToFirst();
while(cursor.hasNext()){
//do stuff here
}
cursor.close();
}