Android的SQLite的如何获得特定的列:行值 [英] Android SQLite how to get particular column:row value

查看:189
本文介绍了Android的SQLite的如何获得特定的列:行值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库,我用做一个查询它

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

光标mCursor = mapDb.query(MY_TABLE,新的String [] {KEY_ONEID,KEY_TWOID},修剪(+ KEY_TWOID +)!='',NULL,NULL,NULL,NULL );

这在SQL条款的字面意思是:
SELECT OneId,TwoId FROM AUTH WHERE TRIM(TwoId)!=''

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

使用原始的SQL查询,这部作品在我的SQLite的浏览器中显示我有问题的行,所以光标对象应包含相同的结果。

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.

其次在我的Java方法,我用一个条件来检查这个结果有什么

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

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

但由于某些原因,即使mCursor散列映射应具有所有返回的值,下一个条件语句

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

mCursor.getString(mCursor.getColumnIndex(KEY_ONEID))。包括(ID)

仍然返回:发生异常:android.database.CursorIndexOutOfBoundsException

这是抛出异常的行: mCursor.getString(mCursor.getColumnIndex(KEY_ONEID))也是如此我在return语句下一个项目

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

我在如何检索特定的行值,则百思不得其解!因为我已经拉了数据库并运行相同的查询,可以清楚地看到,无论我想要的值,这样做事实上是存在的!

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!

光标功能并没有提供许多其他的方式来检索值,因此洞察AP preciated!

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

推荐答案

您需要将光标定位到第一行之前,你可以从它那里得到的数据。要做到这一点,你可以拨打: mCursor.moveToFirst()。此外,该方法调用返回一个布尔值,如果没有结果,这将是虚假的;所以你也可以用它来防止这种情况。

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.

如果您需要通过多个结果迭代,然后调用后 mCursor.moveToFirst(),您可以使用 mCursor.moveToNext()方法,通过结果行一个接一个。再次,当它到达的数据集的结尾这个返回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.

希望是有道理的。

这篇关于Android的SQLite的如何获得特定的列:行值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆