Android Question Cursor problem reading from database

mangojack

Expert
Licensed User
Longtime User
To aid a member ,I have nocked up a small app in an effort to demonstrate methods of getting images from the camera and gallery, and saving them to a database for future viewing. Basic but it serves its pupose.
This app worked great on one devise but is throwing a Cursor error on another.

debugging in exactly same way on both devices sheds no light
the app has been uninstalled. The db has been deleted and replace with a new db.
logs show there is datarows. I have viewed the db with external viewer. I am at a loss what to try now.

error ....
java.lang.IllegalStateException: Couldn't read row 0, col 0 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
this is the section of code .. Cursor1 is dimmed in Process Globals and is Closed after each reference
B4X:
Sub btnViewDBImages_Click
'
'
'

'populate the listview with db records .. data and images ...
   Cursor1 = SQL1.ExecQuery("SELECT * FROM Persons")
   Dim RecID, Fname, Lname As String
   Dim  numRec As Int = Cursor1.RowCount

   Log (numRec)  '@@ shows records ..
   'if there is records in the db then list them
   If numRec  > 0 Then

     For  i = 0 To numRec -1
       Cursor1.Position = i

       RecID = Cursor1.GetInt("ID")  '@@@ This is the ERROR
       Fname = Cursor1.GetString("FirstName")
       Lname = Cursor1.GetString("LastName")

I have uploded the project if anyone is interested (It was being uploaded eventually anyway), you only have to take/get 1 image and save to db.. then 'View DB' to get the error (or not).

Regards mj
 

Attachments

  • Images2DB Example.zip
    11.8 KB · Views: 197
Last edited:

stevel05

Expert
Licensed User
Longtime User
I think if you close the cursor, you'll need to re Dim it.

I can't test it in your app because the database is missing from the zip.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
@stevel05: Actually you can create the database and table. MJ has this line commented. If you uncomment it , you can create the database.db and its table:
B4X:
SQL1.ExecNonQuery("CREATE TABLE Persons (ID INTEGER PRIMARY KEY, FirstName TEXT , LastName TEXT, Image BLOB)")
@mj: I have installed it and ran it on a Galaxy tab2 7 inch. So far it works well. I created 3 records. So far so good if you do not exit the program. Still testing. You need to close the cursor1 and SQL object when you exit the database and program, because I think it returns an error when you get back in the program if you do not.It is a very good sample by the way. Don't smash any kangaroos on the road.
 
Last edited:
Upvote 0

mangojack

Expert
Licensed User
Longtime User
lol .. Many thanks Mahares. I have refreshed the zip to include the db.

I have gone back after Stevel05's post and tried all connotations of dimming /closing cursor but the bad device (Samsung Galaxy S2 4.2.1) does not want to budge.

Edit .. Updated zip to fix the error you highlighted Mahares .. thanks

Thanks again cheers
 
Last edited:
Upvote 0

Itila Tumer

Active Member
Licensed User
Longtime User
It is hard to explaining, I'll try .there is a part of code which is name of " A.txt "
I have ADD code in "B.txt" and UPDATE" code in C.txt.

when I click add ( I have add_click) Add I want to call

and works synchronous
and I want to reach it from onother sub .. how could I do that.
By the way Activity2 is my first activity. And next main menu comes. Just infortmation.

Basicly I want to use this sub code where I want to . How could I do that???
B4X:
 Sub Chooser_Result (Success As Boolean, Dir As String, FileName As String)
.
.
If Success Then

        'display the chosen image in ImageView
        Dim bmp As Bitmap
        bmp.Initialize(Dir, FileName)
        ImageView1.Bitmap = bmp

        'Ask user if they want to save the image to DB ..
    
            'convert the image file to a bytes array

            Dim InputStream1 As InputStream
            InputStream1 = File.OpenInput(Dir, FileName)

            Dim OutputStream1 As OutputStream
            OutputStream1.InitializeToBytesArray(1000)

            File.Copy2(InputStream1, OutputStream1)

            Dim Buffer() As Byte 'declares an empty array
            Buffer = OutputStream1.ToBytesArray
        'If I want to Insert I want to go to Sub ADD
           Activity2.SQL1.ExecNonQuery2("INSERT INTO persons VALUES( NULL, NULL,NULL, NULL,NULL, NULL, NULL, 'Resim')", Array As Object(Buffer))

            'If we click Update
            'Query ="UPDATE persons Set Resim = ?  WHERE ID = " & Activity2.IDList.Get(Activity2.CurrentIndex)
            'Activity2.SQL1.ExecNonQuery2(Query, Array As Object(Buffer))
End Sub
 

Attachments

  • A.txt
    1.3 KB · Views: 191
  • B.txt
    2.6 KB · Views: 201
  • C.txt
    787 bytes · Views: 200
Upvote 0

Itila Tumer

Active Member
Licensed User
Longtime User
I handle INSERT INTO . and how can I manage both of them individuualy.

B4X:
Sub Chooser_Result (Success As Boolean, Dir As String, FileName As String)
' Dim Query As String


    If Success Then

        'display the chosen image in ImageView
        Dim bmp As Bitmap
        bmp.Initialize(Dir, FileName)
        ImageView1.Bitmap = bmp

        'Ask user if they want to save the image to DB ..

            'convert the image file to a bytes array

            Dim InputStream1 As InputStream
            InputStream1 = File.OpenInput(Dir, FileName)

            Dim OutputStream1 As OutputStream
            OutputStream1.InitializeToBytesArray(1000)

            File.Copy2(InputStream1, OutputStream1)

            Dim Buffer() As Byte 'declares an empty array
            Buffer = OutputStream1.ToBytesArray

That is first photo input /output stream as you know.

Below code, the problem how could I control INSERT and UPDATE

B4X:
          ' ADD  INSERT INTO part
      
            Dim ID As String  
            Dim Query As String
            Dim Cursor1 As Cursor
            Query = "SELECT Resim FROM persons WHERE  ID  = " & Activity2.IDList.Get(Activity2.CurrentIndex)
            Cursor1 = Activity2.SQL1.ExecQuery(Query)
            For i=0 To Cursor1.RowCount-1
            Cursor1.Position = i

            ID = Activity2.SQL1.ExecQuerySingleResult("SELECT max(ID) FROM persons")
            Activity2.RowNumber = Activity2.RowNumber + 1                    'increase the row count
            Activity2.IDList.Add(ID)                                        'add the last ID to the list
            Activity2.CurrentIndex = Activity2.IDList.Size - 1        'set the current index to the last one

            Activity2.SQL1.ExecNonQuery2("INSERT INTO persons VALUES( NULL, NULL,NULL, NULL,NULL, NULL, NULL, ?)", Array As Object(Buffer))
  
      
            'UPDATE  part
      
            Query ="UPDATE persons Set Resim = ?  WHERE ID = " & Activity2.IDList.Get(Activity2.CurrentIndex)
            Activity2.SQL1.ExecNonQuery2(Query, Array As Object(Buffer))
 
Last edited:
Upvote 0
Top