That's because your query has an expression, max(RoomSuffix).

The result (cursor) also uses that expression as the column name, and not just "RoomSuffix".

You could do one of the following to fix this:

- Call c.getInt(1) without c.getColumnIndex, since you know there is only one column.

- Rewrite the query as "SELECT MAX(RoomSuffix) AS MAX_SUFFIX FROM RoomTable"

This assigns a known column name (MAX_SUFFIX) to the expression.

You then can call getColumnIndex("MAX_SUFFIX").

- Use SQLiteStatement.simpleQueryForLong with your query:

public long simpleQueryForLong ()
Since: API Level 1

Execute a statement that returns a 1 by 1 table with a numeric value. For example, SELECT COUNT(*) FROM table;
Returns
The result of the query.

-- Kostya

29.11.2010 11:15, pramod.deore пишет:
Why I am getting this exception? RoomSuffix is an int. Then why I got
error when I execute
suf = c45.getInt(c45.getColumnIndex("RoomSuffix"));

Thanks

On Nov 29, 11:55 am, "pramod.deore"<[email protected]>  wrote:
Hi, I want to retrieve int  value from cursor

Cursor c = sampleDB.rawQuery("select max(RoomSuffix)from
RoomTable",null);

and here RoomSuffix is an int, but when I tried to retrieve value as

if (c != null )
                                 {
                                         if  (c.moveToFirst())
                                         {
                                                 do
                                                 {
                                                         System.out.println ("Inside 
do");
                                                         System.out.println 
(c.getColumnCount());    //here i
get 1
                                                         suf = 
c.getInt(c.getColumnIndex("RoomSuffix"));//
here it throws Exception

                                                 }
                                                 while (c.moveToNext());
                                         }
                                 }

and logcat output is

11-29 12:07:28.835: ERROR/CursorWindow(956): Bad request for field
slot 0,-1. numRows = 1, numColumns = 1
11-29 12:07:28.843: WARN/System.err(956):
java.lang.IllegalStateException: get field slot from row 0 col -1
failed
11-29 12:07:28.894: WARN/System.err(956):     at
android.database.CursorWindow.getLong_native(Native Method)
11-29 12:07:28.904: WARN/System.err(956):     at
android.database.CursorWindow.getInt(CursorWindow.java:434)
11-29 12:07:28.925: WARN/System.err(956):     at
android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:
93)
11-29 12:07:28.925: WARN/System.err(956):     at
com.monarch.home.AddRoom.checkRoomExist(AddRoom.java:110)
11-29 12:07:28.944: WARN/System.err(956):     at
com.monarch.home.AddRoom.insertToRoomTable(AddRoom.java:142)
11-29 12:07:28.954: WARN/System.err(956):     at
com.monarch.home.AddRoom$1.onItemClick(AddRoom.java:74)
11-29 12:07:28.966: WARN/System.err(956):     at
android.widget.AdapterView.performItemClick(AdapterView.java:284)
11-29 12:07:28.974: WARN/System.err(956):     at
android.widget.ListView.performItemClick(ListView.java:3285)
11-29 12:07:28.974: WARN/System.err(956):     at
android.widget.AbsListView$PerformClick.run(AbsListView.java:1640)
11-29 12:07:28.996: WARN/System.err(956):     at
android.os.Handler.handleCallback(Handler.java:587)
11-29 12:07:29.005: WARN/System.err(956):     at
android.os.Handler.dispatchMessage(Handler.java:92)
11-29 12:07:29.005: WARN/System.err(956):     at
android.os.Looper.loop(Looper.java:123)
11-29 12:07:29.013: WARN/System.err(956):     at
android.app.ActivityThread.main(ActivityThread.java:4363)
11-29 12:07:29.027: WARN/System.err(956):     at
java.lang.reflect.Method.invokeNative(Native Method)
11-29 12:07:29.035: WARN/System.err(956):     at
java.lang.reflect.Method.invoke(Method.java:521)
11-29 12:07:29.044: WARN/System.err(956):     at
com.android.internal.os.ZygoteInit
$MethodAndArgsCaller.run(ZygoteInit.java:860)
11-29 12:07:29.053: WARN/System.err(956):     at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
11-29 12:07:29.053: WARN/System.err(956):     at
dalvik.system.NativeStart.main(Native Method)


--
Kostya Vasilyev -- WiFi Manager + pretty widget -- http://kmansoft.wordpress.com

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to