SQLite查询没有返回任何内容,movetoFirst方法在Activity中成功,但在IntentService中,给出了错误

时间:2012-10-29 06:31:16

标签: java android sqlite intentservice

当我查询某些内容但它没有返回内容时,if(cursor.moveToFirst()){// logic} 在一个活动中运行良好,但在意图服务中使用它时也不能说...我不知道什么是错的,当查询返回一些东西时(选择CLASSROOM_AVAILABLE的值为0的所有行),特定行的“CLASSROOM_AVAILABLE”将从0到1,如果它什么都不返回,它会将所有行'CLASSROOM_AVAILABLE从1变为2 ..

package my.skul;

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class CheckClassroom extends IntentService {

            /**
             * A constructor is required, and must call the super IntentService(String)
             * constructor with a name for the worker thread.
             */
            public CheckClassroom() {
                super("CheckClassroom");
            }

            /**
             * The IntentService calls this method from the default worker thread with
             * the intent that started the service. When this method returns,
             * IntentService stops the service, as appropriate.
             */
            @Override
            protected void onHandleIntent(Intent intent) {


                open();
                getColumns();
                close();


            }
            private DbHelper ourHelper;
            private SQLiteDatabase ourDatabase;


            public void open() {
                ourHelper = new DbHelper();
                ourDatabase = ourHelper.getWritableDatabase();
            }


            public void close() {
                ourHelper.close();
            }

            private class DbHelper extends SQLiteOpenHelper {

                public DbHelper() {
                    super(CheckClassroom.this, "myschool", null, 1);
                    // TODO Auto-generated constructor stub
                }

                @Override
                public void onCreate(SQLiteDatabase arg0) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
                    // TODO Auto-generated method stub

                }
            }

            public void getColumns()
            {


                String[] columns = new String[] {"CLASSROOM_ID"};
                Cursor c = ourDatabase.query("CLASSROOM_TABLE", columns, "CLASSROOM_AVAILABLE =  0", null,
                        null, null, null);

                if (c.moveToFirst())
                        {
                    int[] myid = new int[c.getCount()];
                    int i = 0;

                    while (!c.isAfterLast()) 
                        {

                        myid[i]=c.getInt(0);
                        ContentValues cv = new ContentValues();
                        cv.put("CLASSROOM_AVAILABLE", 1);
                        ourDatabase.update("CLASSROOM_TABLE", cv, "CLASSROOM_ID = "+myid[i], null);



                        i++;
                        c.moveToNext();

                        }
                        }

                        else
                        {
                            ContentValues cv = new ContentValues();
                            cv.put("CLASSROOM_AVAILABLE", 2);
                            ourDatabase.update("CLASSROOM_TABLE", cv, "CLASSROOM_ID =1", null);
                        }
                }

                }

这是logcat

10-29 12:04:23.903: D/AndroidRuntime(22782): Shutting down VM
10-29 12:04:23.903: W/dalvikvm(22782): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
10-29 12:04:23.923: E/AndroidRuntime(22782): FATAL EXCEPTION: main
10-29 12:04:23.923: E/AndroidRuntime(22782): java.lang.IllegalStateException: Could not execute method of the activity
10-29 12:04:23.923: E/AndroidRuntime(22782):    at android.view.View$1.onClick(View.java:2072)
10-29 12:04:23.923: E/AndroidRuntime(22782):    at android.view.View.performClick(View.java:2408)
10-29 12:04:23.923: E/AndroidRuntime(22782):    at android.view.View$PerformClick.run(View.java:8816)
10-29 12:04:23.923: E/AndroidRuntime(22782):    at android.os.Handler.handleCallback(Handler.java:587)
10-29 12:04:23.923: E/AndroidRuntime(22782):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-29 12:04:23.923: E/AndroidRuntime(22782):    at android.os.Looper.loop(Looper.java:123)
10-29 12:04:23.923: E/AndroidRuntime(22782):    at android.app.ActivityThread.main(ActivityThread.java:4627)
10-29 12:04:23.923: E/AndroidRuntime(22782):    at java.lang.reflect.Method.invokeNative(Native Method)
10-29 12:04:23.923: E/AndroidRuntime(22782):    at java.lang.reflect.Method.invoke(Method.java:521)
10-29 12:04:23.923: E/AndroidRuntime(22782):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-29 12:04:23.923: E/AndroidRuntime(22782):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-29 12:04:23.923: E/AndroidRuntime(22782):    at dalvik.system.NativeStart.main(Native Method)
10-29 12:04:23.923: E/AndroidRuntime(22782): Caused by: java.lang.reflect.InvocationTargetException
10-29 12:04:23.923: E/AndroidRuntime(22782):    at my.skul.Mainmenu.sectwenty(Mainmenu.java:71)
10-29 12:04:23.923: E/AndroidRuntime(22782):    at java.lang.reflect.Method.invokeNative(Native Method)
10-29 12:04:23.923: E/AndroidRuntime(22782):    at java.lang.reflect.Method.invoke(Method.java:521)
10-29 12:04:23.923: E/AndroidRuntime(22782):    at android.view.View$1.onClick(View.java:2067)
10-29 12:04:23.923: E/AndroidRuntime(22782):    ... 11 more
10-29 12:04:23.923: E/AndroidRuntime(22782): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
10-29 12:04:23.923: E/AndroidRuntime(22782):    at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
10-29 12:04:23.923: E/AndroidRuntime(22782):    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
10-29 12:04:23.923: E/AndroidRuntime(22782):    at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:99)
10-29 12:04:23.923: E/AndroidRuntime(22782):    at my.skul.MyCreateClassrooms.get(MyCreateClassrooms.java:111)
10-29 12:04:23.923: E/AndroidRuntime(22782):    ... 15 more

这是相同的代码,在Activity中完美地正常工作..

package my.skul;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class MyCreateClassrooms {

    private static final String DATABASE_NAME = "myschool";
    private static final int DATABASE_VERSION = 1;

    private DbHelper ourHelper;
    private final Context ourContext;
    private SQLiteDatabase ourDatabase;

    private static class DbHelper extends SQLiteOpenHelper {

        public DbHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            // TODO Auto-generated constructor stub
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub

            db.execSQL("CREATE TABLE MAIN_TABLE (OURID INTEGER PRIMARY KEY AUTOINCREMENT, date DATETIME);");

            db.execSQL("CREATE TABLE CLASSROOM_TABLE (CLASSROOM_ID INTEGER PRIMARY KEY AUTOINCREMENT,"
                    + "CLASSROOM_AVAILABLE INTEGER , "
                    + "CLASSROOM_COMPLETION DATETIME);  ");

            db.execSQL("INSERT INTO MAIN_TABLE (date) VALUES (DATETIME('now'))");

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
            // TODO Auto-generated method stub
            db.execSQL("DROP TABLE IF EXIST MAIN_TABLE");

            onCreate(db);

        }

    }

    public MyCreateClassrooms(Context c) {
        ourContext = c;
    }

    public MyCreateClassrooms open() {
        ourHelper = new DbHelper(ourContext);
        ourDatabase = ourHelper.getWritableDatabase();

        return this;

    }

    public void close() {
        ourHelper.close();
    }





     public void getColumns()
     {


            String[] columns = new String[] {"CLASSROOM_ID"};
            Cursor c = ourDatabase.query("CLASSROOM_TABLE", columns, "CLASSROOM_AVAILABLE =  0", null,
                    null, null, null);

            if (!c.moveToFirst())
                    {
                int[] myid = new int[c.getCount()];
                int i = 0;

                while (!c.isAfterLast()) 
                    {

                    myid[i]=c.getInt(0);
                    ContentValues cv = new ContentValues();
                    cv.put("CLASSROOM_AVAILABLE", 1);
                    ourDatabase.update("CLASSROOM_TABLE", cv, "CLASSROOM_ID = "+myid[i], null);



                    i++;
                    c.moveToNext();

                    }
                    }

                    if(c.moveToFirst())
                    {
                        ContentValues cv = new ContentValues();
                        cv.put("CLASSROOM_AVAILABLE", 2);
                        ourDatabase.update("CLASSROOM_TABLE", cv, null, null);
                    }
            }
}

0 个答案:

没有答案