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.
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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