我对这段代码有点问题:
public static int localUserIdByServerUserId(int serverUserId, String serverName){
dbHelper = new DataBaseHelper(context, "opalqnka_sys_tpl.sqlite", null, 1);
dbHelper.getDatabase();
dbHelper.executeQuery("users", "objectId", "2");
dbHelper.executeQuery("users","serverName","opalqnka");
dbHelper.executeQuery("users", "objectId", "3");
dbHelper.executeQuery("users","serverName","opalqnka");
dbHelper.executeQuery("users", "objectId", "3");
String query = "SELECT id FROM users WHERE objectId = "+serverUserId+" AND serverName = '"+serverName+"' LIMIT 1";
ArrayList<String> result = new ArrayList<String>();
cursor = dbHelper.executeSQLQuery(query);
cursor.moveToFirst();
while(!cursor.isAfterLast()) {
result.add(cursor.getString(cursor.getColumnIndex("id")));
cursor.moveToNext();
}
int uuid = Integer.parseInt(result.get(cursor.getInt(cursor.getColumnIndex("id"))));
Log.w("localUSerByIdServerUserId","LocalUserByIdServerUserId result : "+uuid);
cursor.close();
return uuid;
}
在这一行给我android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
:
int uuid = Integer.parseInt(result.get(cursor.getInt(cursor.getColumnIndex("id"))));
我对数据库并不熟悉,这就是为什么我在这里问愚蠢的问题。 所以,如果有人能解决我的问题,请提前感谢!
答案 0 :(得分:2)
首先清楚你想要什么。 崩溃的问题发生在结果中没有得到任何价值。 首先检查记录添加结果。像这样打印日志
Log.e("",""+result.toString());
如果只有一条记录,你可以这样:
int uuid = Integer.parseInt(result.get(0));
如果有多个记录,您可以使用for循环并获得多个ID:
for(int i=0; i<result.size();i++)
int uuid = Integer.parseInt(result.get(i));
答案 1 :(得分:0)
您可以这样做:
cursor.moveToFirst();
while(!cursor.isAfterLast()) {
result.add(cursor.getString(cursor.getColumnIndex("id")));
cursor.moveToNext();
}
Log.i("result ","Result : "+result.toString());
Log.i("CURSOR ","Cursor Position : "+cursor.getPosition());
Integer uuid = Integer.parseInt(result.get(cursor.getColumnIndex("id")));