Why I'm getting an error after deleting a single row from an android sqlite table











up vote
-2
down vote

favorite












After deleting a row from my sqlite database I get always an error if i browse the previous rows and come across the deleted row. it seems that the data is deleted however the _id still existing.
here's my code:



this.database = openHelper.getWritableDatabase();
String table = "flash_table";
String whereClause = "Chap_ID=? AND Flash_Chap_Rank =?" ;
String whereArgs = new String {""+cid, ""+fid};
database.delete(table, whereClause, whereArgs);
database.execSQL("UPDATE SQLITE_SEQUENCE SET seq = (SELECT MAX('Flash_ID')-1) WHERE name = 'flash_table';");


here's the error message:



E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.newapp.flash, PID: 2579
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:151)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:65)


Can anyone help to overcome this issue.










share|improve this question
























  • Would you mind sharing with us what error you’re getting? And how are you deleting the row?
    – Sami Kuhmonen
    Nov 7 at 17:13












  • @SamiKuhmonen I updated my question
    – AndroPro
    Nov 7 at 17:28















up vote
-2
down vote

favorite












After deleting a row from my sqlite database I get always an error if i browse the previous rows and come across the deleted row. it seems that the data is deleted however the _id still existing.
here's my code:



this.database = openHelper.getWritableDatabase();
String table = "flash_table";
String whereClause = "Chap_ID=? AND Flash_Chap_Rank =?" ;
String whereArgs = new String {""+cid, ""+fid};
database.delete(table, whereClause, whereArgs);
database.execSQL("UPDATE SQLITE_SEQUENCE SET seq = (SELECT MAX('Flash_ID')-1) WHERE name = 'flash_table';");


here's the error message:



E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.newapp.flash, PID: 2579
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:151)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:65)


Can anyone help to overcome this issue.










share|improve this question
























  • Would you mind sharing with us what error you’re getting? And how are you deleting the row?
    – Sami Kuhmonen
    Nov 7 at 17:13












  • @SamiKuhmonen I updated my question
    – AndroPro
    Nov 7 at 17:28













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











After deleting a row from my sqlite database I get always an error if i browse the previous rows and come across the deleted row. it seems that the data is deleted however the _id still existing.
here's my code:



this.database = openHelper.getWritableDatabase();
String table = "flash_table";
String whereClause = "Chap_ID=? AND Flash_Chap_Rank =?" ;
String whereArgs = new String {""+cid, ""+fid};
database.delete(table, whereClause, whereArgs);
database.execSQL("UPDATE SQLITE_SEQUENCE SET seq = (SELECT MAX('Flash_ID')-1) WHERE name = 'flash_table';");


here's the error message:



E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.newapp.flash, PID: 2579
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:151)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:65)


Can anyone help to overcome this issue.










share|improve this question















After deleting a row from my sqlite database I get always an error if i browse the previous rows and come across the deleted row. it seems that the data is deleted however the _id still existing.
here's my code:



this.database = openHelper.getWritableDatabase();
String table = "flash_table";
String whereClause = "Chap_ID=? AND Flash_Chap_Rank =?" ;
String whereArgs = new String {""+cid, ""+fid};
database.delete(table, whereClause, whereArgs);
database.execSQL("UPDATE SQLITE_SEQUENCE SET seq = (SELECT MAX('Flash_ID')-1) WHERE name = 'flash_table';");


here's the error message:



E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.newapp.flash, PID: 2579
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:151)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:65)


Can anyone help to overcome this issue.







android database sqlite






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 at 17:28

























asked Nov 7 at 17:12









AndroPro

54




54












  • Would you mind sharing with us what error you’re getting? And how are you deleting the row?
    – Sami Kuhmonen
    Nov 7 at 17:13












  • @SamiKuhmonen I updated my question
    – AndroPro
    Nov 7 at 17:28


















  • Would you mind sharing with us what error you’re getting? And how are you deleting the row?
    – Sami Kuhmonen
    Nov 7 at 17:13












  • @SamiKuhmonen I updated my question
    – AndroPro
    Nov 7 at 17:28
















Would you mind sharing with us what error you’re getting? And how are you deleting the row?
– Sami Kuhmonen
Nov 7 at 17:13






Would you mind sharing with us what error you’re getting? And how are you deleting the row?
– Sami Kuhmonen
Nov 7 at 17:13














@SamiKuhmonen I updated my question
– AndroPro
Nov 7 at 17:28




@SamiKuhmonen I updated my question
– AndroPro
Nov 7 at 17:28












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










You are trying to read a row from a Cursor when there are no rows in the Cursor (you have very likely deleted the single row and hence there are no rows in the table to extract).



The typical cause of this is when the useless check for the Cursor being null is made e.g.



if (cursor != null) { 
cursor.moveToFirst();
cursor.getString(0); //<<<<<<<<< FAIL if cursor is empty
}


This will result in the failure above if there are no rows in the cursor.



This is because a Cursor returned from an SQLiteDatabase method (e.g. query or rawQuery) will never be null. A valid Cursor would be returned.



The moveToFirst method, doesn't fail, instead it returns false if the move could not be accomplished leaving the Cursor at a position of before the first row.



So when you try to get data from the Cursor the exception saying that there are no rows (size is 0) for you to get the data from the column at offset 0.



The fix is to check the return from the moveToFirst. e.g. (instead of the above code)



if (cursor.moveToFirst()) {
cursor.getString(0);
}



  • Note this is the typical code that fails, there are other ways but basically any of the Cursor moveTo????? methods should be checked for the returned value. The Cursor getCount() method can also be used to check if there are any rows in the Cursor.






share|improve this answer























  • Thank you for your reply, the problem is when i delete a row I cannot add any new row in that table, there's no log error however the new data is not recorded on the table.
    – AndroPro
    Nov 8 at 17:32










  • That's is a completely different question, a progression from this one being fixed. After re-ticking this question as answered,as it was, you should ask a question and provide the code relevant to the issue.
    – MikeT
    Nov 8 at 19:46










  • I ticked your previous answer, if you could help regarding the second issue please do.
    – AndroPro
    Nov 9 at 15:12










  • @AndroPro without the relevant code (i.e. based upon the code above) it's impossible to know what's happening. I've eliminated you setting seq, in the sqlite_sequence table to -1 as this does not appear to prohibit insertion. You need to ask a new question with the relevant code and also if you have any triggers those as well.
    – MikeT
    Nov 9 at 19:17











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53194468%2fwhy-im-getting-an-error-after-deleting-a-single-row-from-an-android-sqlite-tabl%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










You are trying to read a row from a Cursor when there are no rows in the Cursor (you have very likely deleted the single row and hence there are no rows in the table to extract).



The typical cause of this is when the useless check for the Cursor being null is made e.g.



if (cursor != null) { 
cursor.moveToFirst();
cursor.getString(0); //<<<<<<<<< FAIL if cursor is empty
}


This will result in the failure above if there are no rows in the cursor.



This is because a Cursor returned from an SQLiteDatabase method (e.g. query or rawQuery) will never be null. A valid Cursor would be returned.



The moveToFirst method, doesn't fail, instead it returns false if the move could not be accomplished leaving the Cursor at a position of before the first row.



So when you try to get data from the Cursor the exception saying that there are no rows (size is 0) for you to get the data from the column at offset 0.



The fix is to check the return from the moveToFirst. e.g. (instead of the above code)



if (cursor.moveToFirst()) {
cursor.getString(0);
}



  • Note this is the typical code that fails, there are other ways but basically any of the Cursor moveTo????? methods should be checked for the returned value. The Cursor getCount() method can also be used to check if there are any rows in the Cursor.






share|improve this answer























  • Thank you for your reply, the problem is when i delete a row I cannot add any new row in that table, there's no log error however the new data is not recorded on the table.
    – AndroPro
    Nov 8 at 17:32










  • That's is a completely different question, a progression from this one being fixed. After re-ticking this question as answered,as it was, you should ask a question and provide the code relevant to the issue.
    – MikeT
    Nov 8 at 19:46










  • I ticked your previous answer, if you could help regarding the second issue please do.
    – AndroPro
    Nov 9 at 15:12










  • @AndroPro without the relevant code (i.e. based upon the code above) it's impossible to know what's happening. I've eliminated you setting seq, in the sqlite_sequence table to -1 as this does not appear to prohibit insertion. You need to ask a new question with the relevant code and also if you have any triggers those as well.
    – MikeT
    Nov 9 at 19:17















up vote
0
down vote



accepted










You are trying to read a row from a Cursor when there are no rows in the Cursor (you have very likely deleted the single row and hence there are no rows in the table to extract).



The typical cause of this is when the useless check for the Cursor being null is made e.g.



if (cursor != null) { 
cursor.moveToFirst();
cursor.getString(0); //<<<<<<<<< FAIL if cursor is empty
}


This will result in the failure above if there are no rows in the cursor.



This is because a Cursor returned from an SQLiteDatabase method (e.g. query or rawQuery) will never be null. A valid Cursor would be returned.



The moveToFirst method, doesn't fail, instead it returns false if the move could not be accomplished leaving the Cursor at a position of before the first row.



So when you try to get data from the Cursor the exception saying that there are no rows (size is 0) for you to get the data from the column at offset 0.



The fix is to check the return from the moveToFirst. e.g. (instead of the above code)



if (cursor.moveToFirst()) {
cursor.getString(0);
}



  • Note this is the typical code that fails, there are other ways but basically any of the Cursor moveTo????? methods should be checked for the returned value. The Cursor getCount() method can also be used to check if there are any rows in the Cursor.






share|improve this answer























  • Thank you for your reply, the problem is when i delete a row I cannot add any new row in that table, there's no log error however the new data is not recorded on the table.
    – AndroPro
    Nov 8 at 17:32










  • That's is a completely different question, a progression from this one being fixed. After re-ticking this question as answered,as it was, you should ask a question and provide the code relevant to the issue.
    – MikeT
    Nov 8 at 19:46










  • I ticked your previous answer, if you could help regarding the second issue please do.
    – AndroPro
    Nov 9 at 15:12










  • @AndroPro without the relevant code (i.e. based upon the code above) it's impossible to know what's happening. I've eliminated you setting seq, in the sqlite_sequence table to -1 as this does not appear to prohibit insertion. You need to ask a new question with the relevant code and also if you have any triggers those as well.
    – MikeT
    Nov 9 at 19:17













up vote
0
down vote



accepted







up vote
0
down vote



accepted






You are trying to read a row from a Cursor when there are no rows in the Cursor (you have very likely deleted the single row and hence there are no rows in the table to extract).



The typical cause of this is when the useless check for the Cursor being null is made e.g.



if (cursor != null) { 
cursor.moveToFirst();
cursor.getString(0); //<<<<<<<<< FAIL if cursor is empty
}


This will result in the failure above if there are no rows in the cursor.



This is because a Cursor returned from an SQLiteDatabase method (e.g. query or rawQuery) will never be null. A valid Cursor would be returned.



The moveToFirst method, doesn't fail, instead it returns false if the move could not be accomplished leaving the Cursor at a position of before the first row.



So when you try to get data from the Cursor the exception saying that there are no rows (size is 0) for you to get the data from the column at offset 0.



The fix is to check the return from the moveToFirst. e.g. (instead of the above code)



if (cursor.moveToFirst()) {
cursor.getString(0);
}



  • Note this is the typical code that fails, there are other ways but basically any of the Cursor moveTo????? methods should be checked for the returned value. The Cursor getCount() method can also be used to check if there are any rows in the Cursor.






share|improve this answer














You are trying to read a row from a Cursor when there are no rows in the Cursor (you have very likely deleted the single row and hence there are no rows in the table to extract).



The typical cause of this is when the useless check for the Cursor being null is made e.g.



if (cursor != null) { 
cursor.moveToFirst();
cursor.getString(0); //<<<<<<<<< FAIL if cursor is empty
}


This will result in the failure above if there are no rows in the cursor.



This is because a Cursor returned from an SQLiteDatabase method (e.g. query or rawQuery) will never be null. A valid Cursor would be returned.



The moveToFirst method, doesn't fail, instead it returns false if the move could not be accomplished leaving the Cursor at a position of before the first row.



So when you try to get data from the Cursor the exception saying that there are no rows (size is 0) for you to get the data from the column at offset 0.



The fix is to check the return from the moveToFirst. e.g. (instead of the above code)



if (cursor.moveToFirst()) {
cursor.getString(0);
}



  • Note this is the typical code that fails, there are other ways but basically any of the Cursor moveTo????? methods should be checked for the returned value. The Cursor getCount() method can also be used to check if there are any rows in the Cursor.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 7 at 19:58

























answered Nov 7 at 19:20









MikeT

13.4k102440




13.4k102440












  • Thank you for your reply, the problem is when i delete a row I cannot add any new row in that table, there's no log error however the new data is not recorded on the table.
    – AndroPro
    Nov 8 at 17:32










  • That's is a completely different question, a progression from this one being fixed. After re-ticking this question as answered,as it was, you should ask a question and provide the code relevant to the issue.
    – MikeT
    Nov 8 at 19:46










  • I ticked your previous answer, if you could help regarding the second issue please do.
    – AndroPro
    Nov 9 at 15:12










  • @AndroPro without the relevant code (i.e. based upon the code above) it's impossible to know what's happening. I've eliminated you setting seq, in the sqlite_sequence table to -1 as this does not appear to prohibit insertion. You need to ask a new question with the relevant code and also if you have any triggers those as well.
    – MikeT
    Nov 9 at 19:17


















  • Thank you for your reply, the problem is when i delete a row I cannot add any new row in that table, there's no log error however the new data is not recorded on the table.
    – AndroPro
    Nov 8 at 17:32










  • That's is a completely different question, a progression from this one being fixed. After re-ticking this question as answered,as it was, you should ask a question and provide the code relevant to the issue.
    – MikeT
    Nov 8 at 19:46










  • I ticked your previous answer, if you could help regarding the second issue please do.
    – AndroPro
    Nov 9 at 15:12










  • @AndroPro without the relevant code (i.e. based upon the code above) it's impossible to know what's happening. I've eliminated you setting seq, in the sqlite_sequence table to -1 as this does not appear to prohibit insertion. You need to ask a new question with the relevant code and also if you have any triggers those as well.
    – MikeT
    Nov 9 at 19:17
















Thank you for your reply, the problem is when i delete a row I cannot add any new row in that table, there's no log error however the new data is not recorded on the table.
– AndroPro
Nov 8 at 17:32




Thank you for your reply, the problem is when i delete a row I cannot add any new row in that table, there's no log error however the new data is not recorded on the table.
– AndroPro
Nov 8 at 17:32












That's is a completely different question, a progression from this one being fixed. After re-ticking this question as answered,as it was, you should ask a question and provide the code relevant to the issue.
– MikeT
Nov 8 at 19:46




That's is a completely different question, a progression from this one being fixed. After re-ticking this question as answered,as it was, you should ask a question and provide the code relevant to the issue.
– MikeT
Nov 8 at 19:46












I ticked your previous answer, if you could help regarding the second issue please do.
– AndroPro
Nov 9 at 15:12




I ticked your previous answer, if you could help regarding the second issue please do.
– AndroPro
Nov 9 at 15:12












@AndroPro without the relevant code (i.e. based upon the code above) it's impossible to know what's happening. I've eliminated you setting seq, in the sqlite_sequence table to -1 as this does not appear to prohibit insertion. You need to ask a new question with the relevant code and also if you have any triggers those as well.
– MikeT
Nov 9 at 19:17




@AndroPro without the relevant code (i.e. based upon the code above) it's impossible to know what's happening. I've eliminated you setting seq, in the sqlite_sequence table to -1 as this does not appear to prohibit insertion. You need to ask a new question with the relevant code and also if you have any triggers those as well.
– MikeT
Nov 9 at 19:17


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53194468%2fwhy-im-getting-an-error-after-deleting-a-single-row-from-an-android-sqlite-tabl%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Academy of Television Arts & Sciences

L'Équipe

1995 France bombings