java Android SQLite 如何获取特定列:行值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6653502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 16:48:52  来源:igfitidea点击:

Android SQLite how to get particular column:row value

javaandroidsqliteexceptioncursor

提问by CQM

I have a database and I am doing a query to it using

我有一个数据库,我正在使用它进行查询

Cursor mCursor = mapDb.query(MY_TABLE, new String[] {KEY_ONEID, KEY_TWOID}, "trim("+KEY_TWOID + ") != '' ", null, null, null, null);

Cursor mCursor = mapDb.query(MY_TABLE, new String[] {KEY_ONEID, KEY_TWOID}, "trim("+KEY_TWOID + ") != '' ", null, null, null, null);

which in SQL terms literally means: SELECT OneId, TwoId FROM auth WHERE trim(TwoId) != ''

在 SQL 术语中,字面意思是: SELECT OneId, TwoId FROM auth WHERE trim(TwoId) != ''

Using the raw SQL query this works in my SQLite browser to show me the rows in question, so the Cursor object should contain the same results.

使用原始 SQL 查询可以在我的 SQLite 浏览器中显示有问题的行,因此 Cursor 对象应该包含相同的结果。

Secondly in my java method I am using a condition to check if this result has anything

其次在我的java方法中,我使用一个条件来检查这个结果是否有任何东西

    if(mCursor.getColumnIndex(KEY_ONEID) > -1)  //if > -1 then the mCursor returned a row
    {

       if(mCursor.getString(mCursor.getColumnIndex(KEY_ONEID)).contains(id)) //breaks here
                return mCursor.getString(mCursor.getColumnIndex(KEY_TWOID));
            else
                return "";
    }

But for some reason, even though the mCursor hashmap should have all the values returned, this next conditional statement

但是出于某种原因,即使 mCursor 哈希图应该返回所有值,下一个条件语句

mCursor.getString(mCursor.getColumnIndex(KEY_ONEID)).contains(id)

mCursor.getString(mCursor.getColumnIndex(KEY_ONEID)).contains(id)

still returns: An exception occurred: android.database.CursorIndexOutOfBoundsException

仍然返回: An exception occurred: android.database.CursorIndexOutOfBoundsException

this is the line that throws the exception: mCursor.getString(mCursor.getColumnIndex(KEY_ONEID))and so does my next item in the return statement

这是引发异常的行:mCursor.getString(mCursor.getColumnIndex(KEY_ONEID))return 语句中的下一项也是如此

I'm baffled at how to retrieve a particular row value then! because I have pulled the database and run the same query and can clearly see that both of the values I want, do in fact exist!

我对如何检索特定行值感到困惑!因为我已经拉取了数据库并运行了相同的查询,并且可以清楚地看到我想要的两个值确实存在!

the Cursor function doesn't provide many other ways to retrieve values, so insight appreciated!

Cursor 函数没有提供许多其他方法来检索值,因此非常感谢!

回答by stephendnicholas

You need to position the Cursor to the first row before you can get data from it. To do this you can call: mCursor.moveToFirst(). Also, this method call returns a boolean, which will be false if there are no results; so you can also use it to guard against that case.

您需要先将 Cursor 定位到第一行,然后才能从中获取数据。要做到这一点,你可以拨打:mCursor.moveToFirst()。此外,此方法调用返回一个布尔值,如果没有结果,它将为 false;所以你也可以用它来防范这种情况。

If you need to iterate through multiple results, then after calling mCursor.moveToFirst(), you can use the mCursor.moveToNext()method go through the result rows one by one. Once again, this returns false when it reaches the end of the data set.

如果需要遍历多个结果,那么调用之后mCursor.moveToFirst(),可以使用mCursor.moveToNext()遍历结果行的方法。再一次,当它到达数据集的末尾时返回 false。

Hope that makes sense.

希望这是有道理的。

回答by Lalit Sharma

You can iterate the cursor like this...

你可以像这样迭代光标......

Cursor c=dbhelper.getQueById(i);
if(c.getCount()>0)
{
    for(c.moveToFirst();!c.isAfterLast();c.moveToNext())
    {
        txt1.setText(c.getString(0));
        txt2.setText(c.getString(1));
        txt3.setText(c.getString(2));   

    }
}

else
{
    Log.d(" Null value in cursor ", " null ");
}
c.close();
dbhelper.close();

回答by Femi

Try calling mCursor.moveToFirst()before you try and call mCursor.getString.

在尝试调用mCursor.moveToFirst()之前先尝试调用mCursor.getString

回答by Bilal Shahid

you Just need to call cursor.moveToNext() move to next start from the beginning so there we don't need to call moveToFirst. moveToFirst is actually called in middle of something.

你只需要调用 cursor.moveToNext() 从头开始​​移动到下一个开始,所以我们不需要调用 moveToFirst。moveToFirst 实际上是在某事中间调用的。

while (cursor.moveToNext()) {
//whatever you want to do 
}