B4J Question Displays data from the database

SMOOTSARA

Active Member
Licensed User
Longtime User
I want to display the values. I receive an error message

B4X:
Waiting for debugger to connect...
Program started.
1
Number of rows = 17821
Cursor.ColumnCount ==> 3
verb_number
verb
verb_view
false
Error occurred on line: 172
java.sql.SQLException: ResultSet closed
    at org.sqlite.RS.checkOpen(RS.java:63)
    at org.sqlite.RS.findColumn(RS.java:108)
    at org.sqlite.RS.getInt(RS.java:293)
    at anywheresoftware.b4j.objects.SQL$ResultSetWrapper.GetInt(SQL.java:504)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:613)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:228)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:90)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:93)
    at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:61)
    at b4j.example.main$ResumableSub_get_verb_html.resume(main.java:183)
    at b4j.example.main._get_verb_html(main.java:168)
    at b4j.example.main._button1_click(main.java:161)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:613)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:231)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:90)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:93)
    at anywheresoftware.b4a.BA$1.run(BA.java:215)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:748)



my code


B4X:
'    db.db
'   
'    tbl_master
'   
'    verb_number       int
'    verb             string
'    verb_view        in
    
    
    Log("Number of rows = " & sql.ExecQuerySingleResult("SELECT count(*) FROM tbl_master"))
    
     Dim Cursor As ResultSet
    Cursor = sql.ExecQuery("SELECT * FROM tbl_master WHERE verb_number="&id)
    Log("Cursor.ColumnCount ==> "&    Cursor.ColumnCount )
    
    Log(Cursor.GetColumnName(0))
    Log(Cursor.GetColumnName(1))
    Log(Cursor.GetColumnName(2))

    Log(Cursor.NextRow)
    
    Do While Cursor.NextRow
        Log(Cursor.GetInt("verb_number"))
        Log(Cursor.GetString("verb"))
        Log(Cursor.GetInt("verb_view"))
    Loop
    Cursor.Close
 
    Log(Cursor.GetInt("verb_number"))
    Log(Cursor.GetString("verb"))
    Log(Cursor.GetInt("verb_view"))


and error is line 172 ==> Log(Cursor.GetInt("verb_number"))
 

udg

Expert
Licensed User
Longtime User
You have a Cursor.NextRow returning False, so the Do-While is skipped, then the Cursor gets closed and finally you try to log GetInt("verb_number") causing the error.
Check the id you use to select the data records.

Definition of ResultSet.NextRow:
"Moves the cursor to the next result. Returns false when the cursor reaches the end."

Update: your query could be written as:
B4X:
Cursor = sql.ExecQuery2($"SELECT * FROM tbl_master WHERE verb_number= ?;"$, Array as Object(id))
 
Upvote 0

SMOOTSARA

Active Member
Licensed User
Longtime User
You have a Cursor.NextRow returning False, so the Do-While is skipped, then the Cursor gets closed and finally you try to log GetInt("verb_number") causing the error.
Check the id you use to select the data records.

Definition of ResultSet.NextRow:
"Moves the cursor to the next result. Returns false when the cursor reaches the end."

hi
There is still a problem
Could you guide the code?
thank you

B4X:
'    db.db
'   
'    tbl_master
'   
'    verb_number       int
'    verb             string
'    verb_view        in
    
    
    Log("Number of rows = " & sql.ExecQuerySingleResult("SELECT count(*) FROM tbl_master"))
    
     Dim Cursor As ResultSet
    Cursor = sql.ExecQuery("SELECT * FROM tbl_master WHERE verb_number="&id)
    Log("Cursor.ColumnCount ==> "&    Cursor.ColumnCount )
    
    Log(Cursor.GetColumnName(0))
    Log(Cursor.GetColumnName(1))
    Log(Cursor.GetColumnName(2))

    Log(Cursor.NextRow)
    
'    Do While Cursor.NextRow
'        Log(Cursor.GetInt("verb_number"))
'        Log(Cursor.GetString("verb"))
'        Log(Cursor.GetInt("verb_view"))
'    Loop
'    Cursor.Close
 
    Log(Cursor.GetInt("verb_number"))
    Log(Cursor.GetString("verb"))
    Log(Cursor.GetInt("verb_view"))



B4X:
Waiting for debugger to connect...
Program started.
1
Number of rows = 17821
Cursor.ColumnCount ==> 3
verb_number
verb
verb_view
false
Error occurred on line: 172
java.sql.SQLException: ResultSet closed
    at org.sqlite.RS.checkOpen(RS.java:63)
    at org.sqlite.RS.findColumn(RS.java:108)
    at org.sqlite.RS.getInt(RS.java:293)
    at anywheresoftware.b4j.objects.SQL$ResultSetWrapper.GetInt(SQL.java:504)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:613)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:228)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:90)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:93)
    at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:61)
    at b4j.example.main$ResumableSub_get_verb_html.resume(main.java:183)
    at b4j.example.main._get_verb_html(main.java:168)
    at b4j.example.main._button1_click(main.java:161)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:613)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:231)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:90)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:93)
    at anywheresoftware.b4a.BA$1.run(BA.java:215)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:748)
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Hi @arastoo ,
it's the same as before. The problem should be the value of "id" . Try to set it manually to a value you know is valid. Eventually use the ExecQuery2 form in my previous post.
 
Last edited:
Upvote 0

RWK

Member
Licensed User
Longtime User
Hi,

B4X:
    Dim Cursor As ResultSet
    Cursor = sql.ExecQuery("SELECT * FROM tbl_master WHERE verb_number="&id)
    
    Log(Cursor.GetColumnName(0))
    Log(Cursor.GetColumnName(1))
    Log(Cursor.GetColumnName(2))

    Log(Cursor.NextRow)   '<<-- this
    
 
    Log(Cursor.GetInt("verb_number"))
    Log(Cursor.GetString("verb"))
    Log(Cursor.GetInt("verb_view"))

Your Query returns 1 row ( i think) then your log the Column Names.....
after that you switching to the next result.....which isn't there (Log: False)
after that there is no cursor to log the results.

B4X:
    Dim Cursor As ResultSet
    Cursor = sql.ExecQuery("SELECT * FROM tbl_master WHERE verb_number="&id)
   
    Log(Cursor.GetColumnName(0))
    Log(Cursor.GetColumnName(1))
    Log(Cursor.GetColumnName(2))
    Log("-----------------------------")
    Log(Cursor.GetInt("verb_number"))
    Log(Cursor.GetString("verb"))
    Log(Cursor.GetInt("verb_view"))

    Log(Cursor.NextRow)
 
Upvote 0
Top