This is a continuation of moving a Web Service API to XOJO 2021r2. However this portion has to specifically do with using a prepared MySQL statement and grabbing data from a MySQL table. That part works and I can iterate through the RowSet that was filled after db.connect and performing a SQLSelect
The part that is not working is a conversion taking the RowSet info and placing that into a JSONItem and returning that back. Then the ToString element of the JSONItem is prepared to be sent back as a response. This worked as a RecordSet but perhaps I do not see something obvious that is preventing this conversion from occurring.
I start by preparing to get data from the MySQL table
// Perform the query.
Var Records As RowSet = db.SelectSQL(SQL, 0 )
I am using the Drummers sqlite table as the model for the MySQL table and named it the same.
SQL is a String which =s SQL = "SELECT * FROM Drummers Where id > ? ORDER BY Votes DESC"
That worked so I perform the JSON conversion
// Convert the RowSet to a JSON object.
Var myJSONInfo As JSONItem = RowSetToJSONItemMySQL( Records )
// Set the response content to a decoded version of the JSON object.
Response.Content = myJSONInfo.ToString
myJSONInfo calls the Method RowSetToJSONItemMySQL
Next I do the following to get JSON info by calling Method RowSetToJSONItemMySQL
// Convert the RowSet to a JSON object.
Var myJSONInfo As JSONItem = RowSetToJSONItemMySQL( Records )
Here is the called Method RowSetToJSONItemMySQL
with Parameters Records As RowSet, Close As Boolean = True
and a Return Type of JSONItem
// Convert a RowSet to JSONItem.
Var RecordsJSON As New JSONItem
// Loop over each record...
While Not Records.AfterLastRow
Var RecordJSON As New JSONItem
// Loop over each column...
For i As Integer = 0 To Records.LastColumnIndex
// Add a name / value pair to the JSON record.
Select Case Records.ColumnType(i)
Case 2, 3, 6, 7, 11, 13, 14, 15, 16, 19
RecordJSON.Value( Records.ColumnAt(i).Name ) = Records.ColumnAt(i).IntegerValue
Case 4, 5, 18
RecordJSON.Value( Records.ColumnAt(i).Name ) = Records.ColumnAt(i).StringValue
Case 8, 9, 10
RecordJSON.Value( Records.ColumnAt(i).Name ) = Records.ColumnAt(i).DateTimeValue
Case 12
RecordJSON.Value( Records.ColumnAt(i).Name ) = Records.ColumnAt(i).BooleanValue
Case Else
RecordJSON.Value( Records.ColumnAt(i).Name ) = Records.ColumnAt(i).Value
End
'Var myname As String = Records.ColumnAt(i).Name ' I did this just to see the data and its there
'Var myvalue As String = Records.ColumnAt(i).Value ' I did this just to see the data and its there
'RecordJSON.Value( Records.ColumnAt(i).Name ) = Records.ColumnAt(i).Value ' Assign data using just name and value which failed
'RecordJSON.Value( myname ) = myvalue ' Did this using the values in myname and myvalue - still failed
Next
// Add the JSON record to the JSON records object.
RecordsJSON.Append(RecordJSON)
// Go to the next row.
Records.MoveToNextRow
Wend
// Close the recordset.
If Close Then
Records.Close
End If
Return RecordsJSON
When this returns to the calling Method this statement
// Set the response content to a decoded version of the JSON object.
Response.Content = myJSONInfo.ToString
fails because ToString has no data.
Now as you can see I modified RowSetToJSONItemMySQL just in case the datatype was the problem meaning the warning DatabaseColumn.Value
Notes
The type-specific properties that get and set the values are recommended over Value.
So just in case I modified that method to check ColumnType and read using the proper type-specific property.
Drummers MySQL table consists of these columns/datatype
So is there again something I missed or do I need to perform this in another manner?