Android Question SQL Cursor Error

shashkiranr

Active Member
Licensed User
Longtime User
Hi All,

I am getting the below error when i read data from the cursor when its position is at 10th row. There are totally 20 rows. It works from 0 to 9 . I checked the DB and it is fine.

B4X:
java.lang.IllegalStateException: Couldn't read row 0, col 0 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.

B4X:
For i = 0 To cu.RowCount-1
        Log(i)
        Dim detail As Ebook
        detail.Initialize
        cu.Position = i
        detail.ID = cu.GetInt("ID")
Next

Any suggestions of how to solve will be very helpful.

Regards,
SK
 

mangojack

Expert
Licensed User
Longtime User
are you sure the db is initialized ?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

shashkiranr

Active Member
Licensed User
Longtime User
I really dont understand. When the position of the cursor goes from 9 to 10, why will it again go to the first row(0) ?
 
Upvote 0

cimperia

Active Member
Licensed User
Longtime User
Just a thought. Is the variable i dim'd at all or is it relying on default? Try to dim it as an integer or a long and see whether it makes any difference?
 
Upvote 0

shashkiranr

Active Member
Licensed User
Longtime User
Here is the full code. I still am not able to solve getting the same error when position is 10. I opened the same DB with ERel Sqlite Viwer and its working. !! :(

B4X:
Ebook_Main_List.Initialize
    If Not(File.Exists(File.DirInternal, "ebook.db3")) Then
        File.Copy(File.DirAssets, "ebook.db3", File.DirInternal, "ebook.db3")
    Else
        File.Delete(File.DirInternal, "ebook.db3")
        File.Copy(File.DirAssets, "ebook.db3", File.DirInternal, "ebook.db3")
    End If
    sq.Initialize(File.DirInternal,"ebook.db3",False)
    Dim cu As Cursor
    cu = sq.ExecQuery("SELECT * FROM Ebook")
    Log(cu.RowCount)
    For i = 0 To cu.RowCount-1
        Log(i)
        Dim detail As Ebook
        detail.Initialize
        cu.Position = i
        detail.ID = cu.GetInt("ID")
        detail.Publisher = cu.GetString("Publisher")
        detail.Title = cu.GetString("Title")
        detail.Category = cu.GetString("Category")
        detail.Downloads = cu.Getint("Downloads")
        detail.Likes = cu.GetInt("Likes")
        detail.Comments = cu.GetInt("Comments")
        detail.Author = cu.GetString("Author")
        detail.Summary = cu.GetString("Summary")
        detail.price = cu.GetString("Price")
       
        DateTime.DateFormat = "MM/dd/yyyy"
        Dim DateDB As Long = DateTime.DateParse(cu.GetString("Date"))
        detail.Date = DateDB
       
        Dim Bmp As Bitmap
        Dim binImage() As Byte, Flux As InputStream
        binImage = cu.GetBlob("Thumbnail")
        Flux.InitializeFromBytesArray(binImage, 0, binImage.Length)
        Bmp.Initialize2(Flux)
        Flux.Close
        detail.Image = Bmp
        Ebook_Main_List.Add(detail)
    Next

Regards,
SK
 
Upvote 0

shashkiranr

Active Member
Licensed User
Longtime User
Got the error. Prob is my database size is huge so the cursor is retrieving all the data hence this error. !! Dumb move !! The below link will explain.
http://stackoverflow.com/questions/...ception-couldnt-read-row-0-col-0-from-cursorw

Edit : I have used Select * , so this will retrieve all the records and the size is more. For anyone who gets this error , check your sql query.

Took me 3 hrs to figure out :(

Regards,
SK
 
Last edited:
Upvote 0

cimperia

Active Member
Licensed User
Longtime User
I think the problem is the size of the thumbnails. If you were to query the database without it, I'm pretty sure there would be no error.

Thank you for reporting the solution.
 
Upvote 0

lkching7

Member
Licensed User
Longtime User
Got the error. Prob is my database size is huge so the cursor is retrieving all the data hence this error. !! Dumb move !! The below link will explain.
http://stackoverflow.com/questions/...ception-couldnt-read-row-0-col-0-from-cursorw

Edit : I have used Select * , so this will retrieve all the records and the size is more. For anyone who gets this error , check your sql query.

Took me 3 hrs to figure out :(

Regards,
SK

No Wonder... Thank You For Your Research...
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
1. I have tested your code and if you issue a LIMIT 10 at the end of your SQL statement, it will display the first 10 records without any issues.
2. As @cimperia mentioned, the BLOB field data is what is slowing you down. If you remove the Thumbnail field from the query, it will run smoothly for all records.
3. You also need to dim the detail inside the for i =0 to......loop like the below. Otherwise you are creating the same object and adding it to the list every time:
For i = 0 To cu.RowCount-1
Log(i)
Dim detail As Ebook
 
Upvote 0
Top