Have I fried my brain?

paul3252

Member
Licensed User
Longtime User
After spending far too much time with B4A this week (well it is addictive), I cannot see why my modified version of the dbutils library function 'executehtml' won't work.

Instead of displaying columns across, and rows in a list I just want to display one record, column names in column 1 and column contents in row 2. I just cannot see the 'issue'.

The error I get is on line marked xxx, when it tries to add the contents of the column in the table. The string build up to that point is fine, it then tells me that I have

"android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1"

There is only one record in the cursor, I know what it means, but I can;t for the life of me see the 'error'.

I know its one of those forehead slapping moments but I can't see the problem.

Sub ExecuteHtml2(SQL As SQL, Query As String, StringArgs() As String, Limit As Int, Clickable As Boolean) As String
Dim Table As List
Dim cur As Cursor
If StringArgs <> Null Then
cur = SQL.ExecQuery2(Query, StringArgs)
Else
cur = SQL.ExecQuery(Query)
End If
Limit = 1
Dim sb As StringBuilder
sb.Initialize
sb.Append("<html><body>").Append(CRLF)
sb.Append("<style type='text/css'>").Append(HtmlCSS).Append("</style>").Append(CRLF)
sb.Append("<table>").Append(CRLF)
For i = 0 To cur.ColumnCount-1
If i Mod 2 = 0 Then
sb.Append("<tr>")
Else
sb.Append("<tr class='odd'>")
End If
sb.Append("<td>").Append(cur.GetColumnName(i)).Append("</td>")
xxx sb.Append("<td>").Append(cur.GetString2(i)).Append("</td>")
sb.Append("</tr>").Append(CRLF)
Next
cur.Close
sb.Append("</table></body></html>")
Return sb.ToString
End Sub
 

paul3252

Member
Licensed User
Longtime User
Eureka, finally saw the problem, was missing a cursor.position statement. Sub now looks like this which may be of use to others.

Sub ExecuteHtml2(SQL As SQL, Query As String, StringArgs() As String, Limit As Int, Clickable As Boolean) As String
Dim Table As List
Dim cur As Cursor
If StringArgs <> Null Then
cur = SQL.ExecQuery2(Query, StringArgs)
Else
cur = SQL.ExecQuery(Query)
End If
Limit = 1
Dim sb As StringBuilder
sb.Initialize
sb.Append("<html><body>").Append(CRLF)
sb.Append("<style type='text/css'>").Append(HtmlCSS).Append("</style>").Append(CRLF)
sb.Append("<table>").Append(CRLF)
cur.Position=0
For i = 0 To cur.ColumnCount-1
If i Mod 2 = 0 Then
sb.Append("<tr>")
Else
sb.Append("<tr class='odd'>")
End If
sb.Append("<td align='Right' width='35%'><b>").Append(cur.GetColumnName(i)).Append("</b></td>")
sb.Append("<td align='Left'>").Append(cur.GetString2(i)).Append("</td>")
'sb.Append("<td align='Left'>The Data</td>")
sb.Append("</tr>").Append(CRLF)
Next
cur.Close
sb.Append("</table></body></html>")
Return sb.ToString
End Sub
 
Upvote 0

joseluis

Active Member
Licensed User
Longtime User
Congratulations. :)

For other times please if you can wrap the code with [ CODE ] [ /CODE ] tags (or the "#" button in the buttonbar) and indent the code you share. It makes it so much easier to read and understand:
B4X:
Sub ExecuteHtml2(SQL As SQL, Query As String, StringArgs() As String, Limit As Int, Clickable As Boolean) As String
   Dim Table As List
   ' (....)
Sub
 
Upvote 0

paul3252

Member
Licensed User
Longtime User
No problem, I couldn't actually see any 'code' tags on the toolbar, just 'quote'. so i'll manually add them next time.
 
Upvote 0

joseluis

Active Member
Licensed User
Longtime User
Yes, there's no such button in the Quick Reply form, one has to go to the Advanced Editing for it to appear.

Hmm... it's funny, the syntax highlighting on the forum just stopped working.
 
Upvote 0

bluejay

Active Member
Licensed User
Longtime User
It is also more useful to 'describe the problem' in the Topic Title so people can more easily find your solution.

cheers

bluejay
 
Upvote 0
Top