Hi, But there is a problem . getColumnIndex("MAX_SUFFIX") doesn't
return the highest integer.
Here is in short what I am doing:-

I have at table which have 3 columns:1. RoomId, 2. RoomaName 3.
RoomSuffix
If suppose in database there are following data
RoomID      RoomName
   1          Hall
   2          Kitchen

and suppose user again add Hall then in that case RoomSuffix must have
to increase by 1.

RoomID     RoomName      RoomSuffix
   1          Hall           0
   2          Kitchen        0
   3          Hall           1
   4          Hall           2

Here I had created a table as

public void insertToRoomTable(String roomName)
{
        try
    {

sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " +
                    ROOM_TABLE_NAME +
                    " (RoomID integer primary key
autoincrement,RoomName VARCHAR,RoomSuffix integer);");

boolean roomExist = checkRoomExist(roomName);

System.out.println ("!...@#!@#...@#!@#"+roomExist);

                System.out.println ("Here value of suf" +suf);

                sampleDB.execSQL("INSERT INTO " +
                                ROOM_TABLE_NAME +
                                " Values (null,'"+roomName+"','"+suf+"');");
        }
                catch (Exception e)
                {
                        e.printStackTrace();
                }
}



public boolean checkRoomExist(String name)
{
        Cursor c = sampleDB.rawQuery("SELECT RoomName FROM "
+ROOM_TABLE_NAME , null);

        if (c != null )
        {
                System.out.println ("%%%%%%%%%%%"+c.getCount());
                if  (c.moveToFirst())
                {
                        do
                        {
                                rname = 
c.getString(c.getColumnIndex("RoomName"));
                                System.out.println ("name is "+name);
                                System.out.println ("name is "+rname);

                                if (name.equalsIgnoreCase(rname))
                                {
                                        System.out.println ("Room already 
exist");
                                        //String str = "select max(RoomSuffix) 
from RoomTable";

                                        Cursor c4 = sampleDB.rawQuery("SELECT 
MAX(RoomSuffix) AS
MAX_SUFFIX FROM RoomTable",null);
                                        if (c4 != null )
                                {
                                        if  (c4.moveToFirst())
                                        {
                                                do
                                                {
                                                        System.out.println 
("***********************");
                                                        System.out.println
(c4.getColumnIndex("MAX_SUFFIX"));
                                                        suf = 
c4.getColumnIndex("MAX_SUFFIX")+1;
                                                        System.out.println 
("!!!!!!!!!!!!"+suf);

                                                }
                                                while (c4.moveToNext());
                                                return true;
                                        }
                                }


                                }
                        }
                        while (c.moveToNext());

                }


        }
        c.close();

        return false;
}

But here is problem is that If there is a record say like

1    Hall   0
2    Hall   1

Upto this ok, But when add another record for Hall then also it enter
it as

3    Hall   1

Means every time

Cursor c4 = sampleDB.rawQuery("SELECT MAX(RoomSuffix) AS MAX_SUFFIX
FROM RoomTable",null);

                                        if (c4 != null )
                                {
                                        if  (c4.moveToFirst())
                                        {
                                                do
                                                {
                                                        System.out.println 
("***********************");
                                                        System.out.println
(c4.getColumnIndex("MAX_SUFFIX"));
                                                        suf = 
c4.getColumnIndex("MAX_SUFFIX")+1;
                                                        //Here it
always returns 0.
                                                        System.out.println 
("!!!!!!!!!!!!"+suf);

                                                }
                                                while (c4.moveToNext());
                                                return true;
                                        }
                                }



Value of suf = 0.


On Nov 29, 3:15 pm, "pramod.deore" <[email protected]> wrote:
> This one_
> - 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").
>
> On Nov 29, 2:28 pm, Kostya Vasilyev <[email protected]> wrote:
>
> > Just curious - which of the three possible fixes did you apply?
>
> > 29.11.2010 12:17, pramod.deore пишет:
>
> > > Thanks Kostya . It works
>
> > > On Nov 29, 1:36 pm, Kostya Vasilyev<[email protected]>  wrote:
> > >> 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
>
> > --
> > 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