开发者

Can't get sqlite to update properly on Android

I am trying to update my sqlite database in Android. I have this function, adjustPosition, which should adjust the position column of my rows based on the position I pass in.

For example, if position=4 and I have two rows with positions 4,5 respectively, then they should be 3,4 by the time the function returns.

public void adjustPosition(int position){
    while(true){
        Cursor query = mDb.rawQuery("select _ID, position from icon where position = " + position, null);
        if(query != null){
            if(query.getCount()==0){
                query.close();
                return;
            }
            query.moveToFirst();
            long id = query.getLong(0);
            int newPositi开发者_如何学运维on = position-1; 
            String sqlQuery = "update icon set position=" + newPosition + " where _ID=" + id;
            boolean result = mDb.rawQuery(sqlQuery, null) != null;
            String sqlQuery2 = "select position from icon where _ID="+id;
            query = mDb.rawQuery(sqlQuery2, null);
            query.moveToFirst();
            int posn = query.getInt(0);  // should equal newPosition, which is 3
            position++; // have a breakpoint here
            query.close();
        } else {                
            return;
        }
    }
}

I have a breakpoint at the line 'positon ++;' . The value of posn should be 3, but for some reason it is 4. I have tried the SQL query using the SQLite Manager plugin in Firefox and it works fine so I don't think my SQL syntax is the problem. Do I have to commit db changes or something?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜