2011-09-20 23 views
0

我有一個小問題,讓在android.My我的數據庫SQLite數據庫元素的Android看起來是這樣的:如何從sqlite的數據 -

id/ serverName/objectId/objectOid/....//objectId is actually userId-which I need to compare 

我是從服務器接收數據我需要檢查從服務器和本地用戶ID從database.I用戶的ID有這樣一段代碼:

public static Integer lUserIdByServerUserId(int serverUserId, String serverName){ 
    return localUserIdByServerUserId(serverUserId, serverName); 

} 

private static Integer localUserIdByServerUserId(int serverUserId, String serverName){ 
    DataBaseHelper dbHelper = new DataBaseHelper(context, "ops_sys_tpl.sqlite",null,1); 
    String query = "SELECT id FROM users WHERE objectId = "+serverUserId+" AND serverName = '"+serverName+"' LIMIT 1"; 
    ArrayList<String> result = new ArrayList<String>(); 
    cursor = dbHelper.executeSQLQuery(query); 
    cursor.moveToFirst(); 
    while(!cursor.isAfterLast()) { 
     result.add(cursor.getString(cursor.getColumnIndex("objectId"))); 
     cursor.moveToNext(); 
    } 

    Log.i("result ","Result : "+result.toString()); 
    Log.i("CURSOR ","Cursor Position : "+cursor.getPosition()); 
    Integer uuid = Integer.parseInt(result.get(cursor.getColumnIndex("objectId"))); 
    Log.w("localUSerByIdServerUserId","LocalUserByIdServerUserId result : "+uuid); 
    cursor.close(); 
    return uuid; 
} 

我試圖檢查數據庫中的所有條目,如果有匹配返回它。我打電話給這樣的方法:

  uuId = rpc.lUserIdByServerUserId(userId,newServerName); 

,我在這行得到這個異常:Integer uuid = Integer.parseInt(result.get(cursor.getColumnIndex("objectId")));

09-20 16:55:47.647: WARN/System.err(31031): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 
09-20 16:55:47.647: WARN/System.err(31031):  at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257) 
09-20 16:55:47.647: WARN/System.err(31031):  at java.util.ArrayList.get(ArrayList.java:311) 
09-20 16:55:47.647: WARN/System.err(31031):  at com.stampii.stampii.comm.rpc.RPCCommunicator.localUserIdByServerUserId(RPCCommunicator.java:1083) 
09-20 16:55:47.647: WARN/System.err(31031):  at com.stampii.stampii.comm.rpc.RPCCommunicator.lUserIdByServerUserId(RPCCommunicator.java:1066) 
09-20 16:55:47.657: WARN/System.err(31031):  at com.stampii.stampii.comm.rpc.InfoStartRPCPacket.executeBefore(InfoStartRPCPacket.java:176) 
09-20 16:55:47.657: WARN/System.err(31031):  at com.stampii.stampii.user.UserLogin$2$1.run(UserLogin.java:212) 
09-20 16:55:47.657: WARN/System.err(31031):  at java.lang.Thread.run(Thread.java:1102) 
09-20 16:55:47.657: WARN/ERROR(31031): Error - java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 

而實際上我在我的數據庫只有一個測試項,這與從服務器的用戶ID的回報。

任何想法,我的錯誤在哪裏以及如何解決?

+0

什麼是(RPCCommunicator.java:1083)?請標記代碼行。 –

+0

抱歉忘了提及它。我只是用正確的線更新我的問題:Integer uuid = Integer.parseInt(result.get(cursor.getColumnIndex(「objectId」))); –

回答

1

問題出在光標的位置上

試試這段代碼將光標定位到起始點。

if (mNotesCursor.moveToFirst()) { 
     do { 
     result.add(cursor.getString(cursor.getColumnIndex("objectId"))); 
     } while (mNotesCursor.moveToNext()); 
} 
+0

我試過了,但還是有例外。 –

+0

在這一行中:Integer uuid = Integer.parseInt(result.get(cursor.getColumnIndex(「objectId」))); –

+0

@Bombastic將該行放入do循環並嘗試。我猜是在獲取所有值之後,光標到達終點位置。現在如果你試圖獲得值它會拋出異常 – Venky