android 将sql查询结果保存为游标异常
我对那段代码有一个小问题:
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 requests, with a size of 0
在这一行:
int uuid = Integer.parseInt(result.get(cursor.getInt(cursor.getColumnIndex("id"))));
我不太熟悉数据库为什么我在这里问也许很愚蠢的问题。 因此,如果有人能解决我的问题,请提前致谢!
I have a little issue with that piece of code :
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;
}
It's throwing me an android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at this line :
int uuid = Integer.parseInt(result.get(cursor.getInt(cursor.getColumnIndex("id"))));
I'm not really familiar with database that's why I'm asking maybe stupid questions here.
So if anybody has a solution for my problem, thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先明确自己想要什么。
崩溃问题是因为没有得到任何结果值而发生的。
首先检查结果中添加的记录。像这样打印日志
如果只有一条记录,您可以这样得到:
如果有不止一条记录,您可以使用 for 循环并获取多个 id:
First clear what you want.
The crashing issue occur coz of not getting any value in result.
first you check the record add in result. Print log like this
If there is only one record the you can get like this:
If there is more than one record the you can use for loop and get more than one ids:
你可以这样做:
You can do something like this :