我在SQLite中获得索引超出范围的异常

时间:2017-11-30 07:34:17

标签: android sqlite android-sqlite sqliteopenhelper

我正在制作一个精确的单词trasnlation应用程序。我在我的sqlite中有一对单词,而我的应用程序只返回一个结果,当一个精确的文字匹配(string by string)被排队。 我的问题是当我发出查询时,我在Android SQLite

中获得了一个索引超出范围的异常
  

android.database.CursorIndexOutOfBoundsException:请求索引0,   大小为0

然后当我尝试选择它抛出的所有记录时

  

W / System.err:android.database.CursorIndexOutOfBoundsException:Index   24要求,大小为24

以下是我的代码:

package com.example.android.cebuano_tagalogtranslator;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    Button btn_clear;
    Button btn_translate_to_ceb;
    Button btn_translate_to_fil;
    EditText txt_input;
    EditText txt_output;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        //HABAGAT CONTROLS BEGIN
        btn_clear = (Button) findViewById(R.id.btn_clear);
        btn_translate_to_ceb = (Button) findViewById(R.id.btn_trans_to_ceb);
        btn_translate_to_fil = (Button) findViewById(R.id.btn_trans_to_fil);

        txt_input = (EditText) findViewById(R.id.input);
        txt_output = (EditText) findViewById(R.id.output);

        //HABAGAT : CLEAR BOTH TEXT
        btn_clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                txt_input.setText("type here");
                txt_output.setText("");
            }
        });

        //HABAGAT : FILIPINO -> CEBUANO
        btn_translate_to_ceb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                try {
                    String textinput = txt_input.getText().toString();
                    textinput = "\""+ textinput +"\"";
                    String filToCebQuery = "SELECT ceb FROM filtoceb WHERE fil = "+ textinput;
                    SQLiteDatabase DB = openOrCreateDatabase("filtoceb", MODE_PRIVATE, null);
                    //Cursor c = DB.rawQuery("SELECT * FROM filtoceb", null);
                    Cursor c = DB.rawQuery(filToCebQuery, null);

                    int cebIndex = c.getColumnIndex("ceb");
                    //int filIndex = c.getColumnIndex("fil");

                    c.moveToFirst();
                    while (c != null) {

                    //Log.i("Results - ceb", c.getString(cebIndex));
                    txt_output.setText(c.getString(cebIndex));
                    c.moveToNext();
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });

        //HABAGAT : CREATE DB OPEN IF NOT CREATED YET
            try {
            SQLiteDatabase eventsDB = this.openOrCreateDatabase("filtoceb", MODE_PRIVATE, null);
            //eventsDB.execSQL("drop table user");
            eventsDB.execSQL("CREATE TABLE IF NOT EXISTS filtoceb (fil VARCHAR, ceb VARCHAR)");

            eventsDB.execSQL("INSERT INTO filtoceb (ceb, fil) VALUES ('Kumusta ka?','Kumusta ka?')");
            eventsDB.execSQL("INSERT INTO filtoceb (ceb, fil) VALUES ('Maayo, salamat', 'Mabuti naman, salamat')");
            eventsDB.execSQL("INSERT INTO filtoceb (ceb, fil) VALUES ('Unsay imong pangalan?',  'Ano pangalan mo?')");
            eventsDB.execSQL("INSERT INTO filtoceb (ceb, fil) VALUES ('Unsay ngalan mo?', 'ano pangalan mo?')");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

我做错了什么?

2 个答案:

答案 0 :(得分:2)

int cebIndex = c.getColumnIndex("ceb");

if (cursor.moveToFirst()){
   while(!cursor.isAfterLast()){
      String data = cursor.getString(cebIndex);
      // do what ever you want here
      txt_output.setText(cursor.getString(cebIndex));
      cursor.moveToNext();
   }
}
cursor.close();

答案 1 :(得分:1)

if (c!=null)
     {
       try {
            c.moveToFirst();
            while(!c.isAfterLast()){
                 String result = c.getString(c.getColumnIndex("ceb"));
                 txt_output.setText(result);
                 c.moveToNext();
               }
            } 
         finally 
            {
              cursor.close();
             }
      }