简体   繁体   中英

Unable to retrieve a single string-data from sqlite table'column which is dynamically created and cause “Index 0 requested, with a size of 0”

But I can able to retrieve a string-data when dynamically created Table and select query are within the same method.

Dynamically creating Table helper.classhomework(tablename,dateString);

public void classhomework(String tablename,String date){

    String COLUMN_ID="S.No";

    String COLUMN_SUBJECT1="subject1"; 

    String COLUMN_SUBJECT2="subject2";

    String COLUMN_SUBJECT3="subject3";

    String COLUMN_dATE="date";

    ContentValues insertValues = new ContentValues();
    SQLiteDatabase db = getWritableDatabase();
    db.execSQL("CREATE TABLE IF NOT EXISTS "+ tablename+ "( " +
        COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
        COLUMN_SUBJECT1 + " TEXT, " +
        COLUMN_SUBJECT2 + " TEXT, " +
        COLUMN_SUBJECT3 + " TEXT, " +
        COLUMN_dATE     + " TEXT " +
        ");");
    insertValues.put(COLUMN_SUBJECT1,"Not updated ");
    insertValues.put(COLUMN_SUBJECT2,"Not updated ");
    insertValues.put(COLUMN_SUBJECT3,"Not updated ");
    insertValues.put(COLUMN_dATE,date);

    db.insert(tablename,null,insertValues);
    db.close();
}

Calling Select Query Method

String homeworkdetails=helper.gethomework(dateString,tablename,subjtxt);

Select Query Method

    public String gethomework(String date,String tablename,String subject){

    String COLUMN_dATE="date";

    SQLiteDatabase db = getWritableDatabase();

    String homeworkcontent = null;
    Cursor cur=db.query(tablename,new String[]{subject},COLUMN_dATE+"=?",new                               String[]{String.valueOf(date)},null,null,null,null);
    if(cur!=null){
        cur.moveToFirst();
  homeworkcontent =cur.getString(0);//**CursorIndexOutOfBoundsException**
    }cur.close();
        db.close();
    return homeworkcontent;

But returned value when table is created inside this method.

Calling Update query

helper.updatehomework(dateString,homeworksubmt,subjectname,tabname)

Update Query

    public boolean updatehomework(String date,String homework,String subject,String table ){
        try{ContentValues updateValues = new ContentValues();
        SQLiteDatabase db = getWritableDatabase();
        updateValues.put(subject,homework);
        db.update(table,updateValues,"date =?",new String[]{date});
         return true;

        }catch (Exception me){
            me.printStackTrace();
        }return false;
    }

values successfully updated.

verified all the table name usages.

This error occur because your cursor has no rows. yo have to check size condition for this.

if(cur!=null  && cur..getCount() > 0)
{
......
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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