Android Question SQL Cursor Error

Discussion in 'Android Questions' started by shashkiranr, Jun 28, 2015.

  1. shashkiranr

    shashkiranr Active Member Licensed 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.

    Code:
    java.lang.IllegalStateException: Couldn't read row 0, col 0 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
    Code:
    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
     
  2. mangojack

    mangojack Well-Known Member Licensed User

    are you sure the db is initialized ?
     
  3. shashkiranr

    shashkiranr Active Member Licensed User

    Yes. The rows from 0 to 9 can be read easily. its in a for loop.
     
  4. DonManfred

    DonManfred Expert Licensed User

    No. It stops reading the first (0).

     
  5. shashkiranr

    shashkiranr Active Member Licensed 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) ?
     
  6. LucaMs

    LucaMs Expert Licensed User

    what are you talking about???
     
    Last edited: Jun 28, 2015
  7. shashkiranr

    shashkiranr Active Member Licensed User

  8. cimperia

    cimperia Active Member Licensed 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?
     
  9. shashkiranr

    shashkiranr Active Member Licensed 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. !! :(

    Code:
    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
     
  10. shashkiranr

    shashkiranr Active Member Licensed 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: Jun 28, 2015
    cimperia likes this.
  11. cimperia

    cimperia Active Member Licensed 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.
     
  12. lkching7

    lkching7 Member Licensed User

    No Wonder... Thank You For Your Research...
     
  13. Mahares

    Mahares Well-Known Member Licensed 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
     
    eps and cimperia like this.
  14. shashkiranr

    shashkiranr Active Member Licensed User

    @Mahares There was a image which was 2.2MB. Thats why it was failing. Still SELECT * is not recommended. Thank you for your effort :)
     
Loading...