android 将sql查询结果保存为游标异常

发布于 2024-12-05 02:41:16 字数 1493 浏览 1 评论 0原文

我对那段代码有一个小问题:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风苍溪 2024-12-12 02:41:16

首先明确自己想要什么。
崩溃问题是因为没有得到任何结果值而发生的。
首先检查结果中添加的记录。像这样打印日志

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));

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

Log.e("",""+result.toString());

If there is only one record the you can get like this:

int uuid = Integer.parseInt(result.get(0));

If there is more than one record the you can use for loop and get more than one ids:

for(int i=0; i<result.size();i++)
    int uuid = Integer.parseInt(result.get(i));
我恋#小黄人 2024-12-12 02:41:16

你可以这样做:

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")));

You can do something like this :

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")));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文