繁体   English   中英

如何使用SQLite解决索引超出范围错误?

[英]How to resolve index out of bounds error with SQLite?

我试图从SQLite数据库获取数据,而当我尝试时,我的应用程序崩溃了,错误是:“ android.database.CursorIndexOutOfBoundsException:请求索引-1,大小为1”。 我究竟做错了什么?

这是我的活动:

public class EventInfo extends AppCompatActivity {
DatabaseHelper myDb;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_event_info);
    myDb=new DatabaseHelper(this);
    SharedPreferences sp = getSharedPreferences("key", 0);
    final String event_title=sp.getString("event_title", "");
    final String event_date=sp.getString("event_date", "");
    String event_content=sp.getString("event_content","");
    String age_limit=sp.getString("age_limit","");
    String event_hour=sp.getString("event_hour","");
    String location_left=sp.getString("location_left","");
    String location_right=sp.getString("location_right","");

    TextView txt5=(TextView)findViewById(R.id.textView5);
    TextView txt6=(TextView)findViewById(R.id.textView6);
    TextView txt7=(TextView)findViewById(R.id.textView7);
    TextView txt8=(TextView)findViewById(R.id.textView8);
    TextView txt9=(TextView)findViewById(R.id.textView9);
    TextView txt10=(TextView)findViewById(R.id.textView10);
    TextView txt14=(TextView)findViewById(R.id.textView14);

    txt5.setText(event_title);
    txt6.setText(event_date);
    txt7.setText(age_limit);
    txt8.setText(event_content);
    txt9.setText(event_hour);
    txt10.setText("Press here for event location");
    String votes_count=myDb.getVotesCount(event_title);
    if(votes_count.equals("1")) {
        txt14.setText("Cancel Attending");
    }
    else {
        if(votes_count.equals("0")||votes_count.isEmpty()) {
            txt14.setText("Accept Attending");
        }
        else
        {
            txt14.setText("Error");
        }
    }
    txt10.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(EventInfo.this, MapsActivity3.class);
            startActivity(intent);

        }
    });



    txt14.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String votes_count=myDb.getVotesCount(event_title);
            if(votes_count.equals("0")||votes_count.isEmpty()) {
                boolean insert = myDb.insertData("1", event_title, event_date);
                if (insert == true)
                    Toast.makeText(EventInfo.this, "You have accepted the arrival", Toast.LENGTH_LONG).show();
                else
                    Toast.makeText(EventInfo.this, "Error", Toast.LENGTH_LONG).show();
            }
            else
            {
                if(votes_count.equals("1"))
                {
                    boolean insert = myDb.insertData("0", event_title, event_date);
                    if (insert == true)
                        Toast.makeText(EventInfo.this, "You have canceled the arrival", Toast.LENGTH_LONG).show();
                    else
                        Toast.makeText(EventInfo.this, "Error, Toast.LENGTH_LONG).show();
                }
                else
                {
                    Toast.makeText(EventInfo.this, votes_count.toString(), Toast.LENGTH_LONG).show();
                }
            }
        }
    });
}

}

这是我的DatabaseHelper类:

public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME="usersDB.db";
public static final String TABLE_NAME="votes_table";
public static final String COL2="VOTESCOUNT";
public static final String COL3="EVENTTITLE";
public static final String COL4="EVENTDATE";


public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);
    SQLiteDatabase db = this.getWritableDatabase();
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table " + TABLE_NAME + " (VOTESCOUNT TEXT, EVENTTITLE TEXT, EVENTDATE TEXT)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
    onCreate(db);
}

public boolean insertData(String votes_count, String event_title, String event_date){
    SQLiteDatabase db =this.getWritableDatabase();
    ContentValues contentValues=new ContentValues();
    contentValues.put(COL2,votes_count);
    contentValues.put(COL3,event_title);
    contentValues.put(COL4, event_date);
    long result=db.insert(TABLE_NAME,null,contentValues);
    if(result==-1)
    {
        return false;
    }
    else
        return true;
}

public Cursor getAllData()
{
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery("select * from "+TABLE_NAME,null);
    return res;
}

public Cursor getEventTitles()
{
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res=db.rawQuery("select EVENTTITLE from "+TABLE_NAME,null);
    return res;
}

public Cursor getEventDate(String eventtitle)
{
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res=db.rawQuery("select EVENTDATE from "+TABLE_NAME+" where EVENTTITLE='"+eventtitle+"'",null);
    return res;
}

public String getVotesCount(String eventtitle)
{
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res=db.rawQuery("select VOTESCOUNT from "+TABLE_NAME+" where EVENTTITLE='"+eventtitle+"'",null);
    String votes_count=res.getString(0);
    return votes_count;
}

}

您必须先将Cursor移到第一行,然后才能从中访问数据,并且最好检查moveToFirst()的返回值以确保Cursor具有数据。 另外,完成所有游标后,请确保关闭它们:

public String getVotesCount(String eventtitle)
{
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res=db.rawQuery("select VOTESCOUNT from "+TABLE_NAME+" where EVENTTITLE='"+eventtitle+"'",null);

    String votes_count = null;
    if (res.moveToFirst()) {
         votes_count =res.getString(0);
    }
    res.close();
    return votes_count;
}

并为返回值添加一个空检查:

String votes_count = myDb.getVotesCount(event_title);
if(votes_count != null && (votes_count.equals("0")||votes_count.isEmpty())) {
//...............

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM