2013-03-02 68 views
0

我在android中用SQLite數據庫簡單函數返回一些記錄的計數。Sqlite - 簡單函數返回計數

public int getThreadMessagesCount(int thread_id){ 

    int mThreadCount = 0 ; 

    Cursor cur = db.rawQuery("select count(*) as ThreadCount from tbl_messaging where thread_id = ?", new String[]{thread_id+""}); 
    Log.d("xxx", "query executed"); 

    if (cur.getCount()<=0) 
     mThreadCount = 0 ; 
     else 
      mThreadCount = cur.getInt(cur.getColumnIndex("ThreadCount")) ; 

    return mThreadCount ; 



} 

但是當執行函數拋出expiation給我。

登錄:

03-02 12:09:03.210: E/AndroidRuntime(32360): FATAL EXCEPTION: main 
03-02 12:09:03.210: E/AndroidRuntime(32360): android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1 
03-02 12:09:03.210: E/AndroidRuntime(32360): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:418) 
03-02 12:09:03.210: E/AndroidRuntime(32360): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136) 
03-02 12:09:03.210: E/AndroidRuntime(32360): at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:68) 
03-02 12:09:03.210: E/AndroidRuntime(32360): at com.example.tawasol1.cls_database.getThreadMessagesCount(cls_database.java:460) 
03-02 12:09:03.210: E/AndroidRuntime(32360): at com.example.tawasol1.Cls_act_check_account$5.onClick(Cls_act_check_account.java:377) 
03-02 12:09:03.210: E/AndroidRuntime(32360): at android.view.View.performClick(View.java:4261) 
03-02 12:09:03.210: E/AndroidRuntime(32360): at android.view.View$PerformClick.run(View.java:17356) 
03-02 12:09:03.210: E/AndroidRuntime(32360): at android.os.Handler.handleCallback(Handler.java:615) 
03-02 12:09:03.210: E/AndroidRuntime(32360): at android.os.Handler.dispatchMessage(Handler.java:92) 
03-02 12:09:03.210: E/AndroidRuntime(32360): at android.os.Looper.loop(Looper.java:137) 
03-02 12:09:03.210: E/AndroidRuntime(32360): at android.app.ActivityThread.main(ActivityThread.java:4921) 
03-02 12:09:03.210: E/AndroidRuntime(32360): at java.lang.reflect.Method.invokeNative(Native Method) 
03-02 12:09:03.210: E/AndroidRuntime(32360): at java.lang.reflect.Method.invoke(Method.java:511) 
03-02 12:09:03.210: E/AndroidRuntime(32360): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038) 
03-02 12:09:03.210: E/AndroidRuntime(32360): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805) 
03-02 12:09:03.210: E/AndroidRuntime(32360): at dalvik.system.NativeStart.main(Native Method) 
+0

發表您的logcat..what例外,你得到什麼?我想你og forogt調用moveToFirst()上的光標 – idanakav 2013-03-02 10:01:26

+0

看到更新的文章 – Realbitt 2013-03-02 10:11:10

+0

你的權利,我忘記movetofirst(); :( 謝謝 – Realbitt 2013-03-02 10:13:29

回答

0

試試這個代碼

public int getThreadMessagesCount(int thread_id){ 

int mThreadCount = 0 ; 

Cursor cur = db.rawQuery("select count(*) as ThreadCount from tbl_messaging where thread_id = ?", new String[]{thread_id+""}); 
Log.d("xxx", "query executed"); 

if (cur.getCount()<=0) 
    mThreadCount = 0 ; 
    else{ 
     cur.moveToNext(); 
     mThreadCount = cur.getInt(cur.getColumnIndex("ThreadCount")) ;} 

return mThreadCount ; 

}`