如何使用光标?

时间:2017-04-21 15:50:55

标签: java android sqlite cursor android-cursor

我有一个应用程序,它接收动物的id并将该id传递给数据库中的方法,该方法将在do-while中多次读取。我想知道为什么会发生这种错误?

这是我的代码:

bd = new BaseDados(getApplicationContext());
        Cursor cc = bd.getIdAnimal(chipnumber);
        if (cc.moveToFirst()) {

        idanimal = cc.getInt(cc.getColumnIndex("idanimal"));
    }

    Cursor ccc = bd.getGruupsnosAnimals(idanimal);

    do{

        if(ccc.moveToFirst()){

            String groupname= ccc.getString(ccc.getColumnIndex("groupname"));
        }


    }while (ccc.moveToNext());

这是我的错误:

   Process: com.example.nobre.myapplication, PID: 4529
                                                                                   android.database.sqlite.SQLiteException: not an error (code 0)
                                                                                       at android.database.sqlite.SQLiteConnection.nativeExecuteForCursorWindow(Native Method)
                                                                                       at android.database.sqlite.SQLiteConnection.executeForCursorWindow(SQLiteConnection.java:845)
                                                                                       at android.database.sqlite.SQLiteSession.executeForCursorWindow(SQLiteSession.java:836)
                                                                                       at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:62)
                                                                                       at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:144)
                                                                                       at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:133)
                                                                                       at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:197)
                                                                                       at android.database.AbstractCursor.moveToFirst(AbstractCursor.java:237)
                                                                                       at com.example.nobre.myapplication.Activities.VerAnimaisActivity.showDialog(VerAnimaisActivity.java:193)
                                                                                       at com.example.nobre.myapplication.Activities.VerAnimaisActivity$2.onClick(VerAnimaisActivity.java:153)
                                                                                       at android.view.View.performClick(View.java:4780)
                                                                                       at android.view.View$PerformClick.run(View.java:19866)
                                                                                       at android.os.Handler.handleCallback(Handler.java:739)
                                                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                       at android.os.Looper.loop(Looper.java:135)
                                                                                       at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                                       at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

3 个答案:

答案 0 :(得分:1)

异常:不是错误?那是一个新的。

你的底部循环有点奇怪,可能是一个错误 - 你正在向下一个移动,但如果失败,甚至可能没有移动到第一个。如果成功,你将无限循环。试试这个:

if(ccc.moveToFirst()){

    do{

        String groupname= ccc.getString(ccc.getColumnIndex("groupname"));
    }while (ccc.moveToNext());


}

答案 1 :(得分:1)

private BaseLoaderCallback  mLoaderCallback = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {
        switch (status) {
            case LoaderCallbackInterface.SUCCESS:
            {
                Log.i("OpenCv", "OpenCV loaded successfully");
                mOpenCvCameraView.enableView();

            } break;
            default:
            {
                super.onManagerConnected(status);
            } break;
        }
    }
};

@Override
public void onResume()
{
    super.onResume();
    if (!OpenCVLoader.initDebug()) {
        Log.d("OpenCv", "Internal OpenCV library not found. Using OpenCV Manager for initialization");
        OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_11, this, mLoaderCallback);
    } else {
        Log.d("OpenCv", "OpenCV library found inside package. Using it!");
        mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
    }
}


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera);

    mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.tutorial1_activity_java_surface_view);
    mOpenCvCameraView.setMaxFrameSize(720,1280);
    mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
    mOpenCvCameraView.setCvCameraViewListener(this);
    }


@Override
public void onCameraViewStarted(int width, int height) {

    // Recieve Image Data when the caera preview starts
    mRgba = new Mat(height, width, CvType.CV_8UC4);
    mRgbaT = new Mat(height, width, CvType.CV_8UC4);
    mRgbaF = new Mat(height, width, CvType.CV_8UC4);
    //End

    mSpectrum = new Mat();
    mBlobColorRgba = new Scalar(255);
    mBlobColorHsv = new Scalar(255);
    SPECTRUM_SIZE = new Size(400, 400);
    CONTOUR_COLOR = new Scalar(255,0,0,255);

}


@Override
public void onCameraViewStopped() {
    mRgba.release();
}

@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {

     timeofFrame = System.currentTimeMillis();

    mRgba = inputFrame.rgba();

    Core.transpose(mRgba,mRgbaT);
    //Rotate 90 degrees//
    Imgproc.resize(mRgbaT,mRgbaF,mRgbaF.size(),0,0,0);
    Core.flip(mRgbaF,mRgba,1);

    return mRgba;

    }
}

这是一个无限循环,当你在光标中有下一个项目时,你移动到第一个项目,下一个迭代你将再次移动到第一个项目。

另外,请不要忘记在使用后拨打do{ if(ccc.moveToFirst()){ String groupname= ccc.getString(ccc.getColumnIndex("groupname")); } }while (ccc.moveToNext());

答案 2 :(得分:-1)

试试这个:

// Move the cursor to the first row  if cursor is not empty
if(ccc.moveToFirst()) {

    do{
          String groupname= ccc.getString(ccc.getColumnIndex("groupname"));
    }while (ccc.moveToNext()); // Move cursor to next row until it pass last entry
}

代替:

do{
       if(ccc.moveToFirst()) {
          String groupname= ccc.getString(ccc.getColumnIndex("groupname"));
       }
}while (ccc.moveToNext());