【问题标题】:I am getting index out of bounds exception in SQLite我在 SQLite 中获取索引越界异常
【发布时间】:2017-11-30 07:34:16
【问题描述】:

我正在制作一个精确的单词翻译应用程序。我的 sqlite 中有一对单词,并且我的应用程序仅在查询完全文字匹配(逐个字符串)时才返回结果。 我的问题是当我发出查询时,我在 Android SQLite 中得到一个索引超出范围异常

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

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

W/System.err: android.database.CursorIndexOutOfBoundsException: 索引 请求 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();
        }
    }
}

我做错了什么?

【问题讨论】:

  • while (c != null) - 这种情况真的没有意义。如果c 为空,它会在到达之前抛出。在开始循环之前进行空值检查。然后,moveToFirst()moveToNext() 都返回一个boolean,指示Cursor 是否可以移动。以这些为基础。

标签: android sqlite android-sqlite sqliteopenhelper


【解决方案1】:
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();

【讨论】:

  • 它不再抛出错误但它可以返回的只是我数据库的第一条记录?当我尝试任何其他词时,什么也没有发生,也没有日志错误?
【解决方案2】:
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();
             }
      }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-08
    • 2012-06-19
    • 2019-03-08
    • 1970-01-01
    • 2018-11-19
    • 2013-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多