Access Resultset without transforming it to Json or XML ballerina.io

Viewed 86

I am trying to access the plain resultset without transforming it to json or XML.

My database transforms the resultset into the correct format.

Example:

var resultSet = testDB->select("SELECT * FROM test , ());
           
            if (resultSet is table<record {}>)
            { 

            }

When I try to run foreach over resultSet I get an empty field.

versions: ballerina --version jBallerina 1.2.8 Language specification 2020R1 Update Tool 0.8.8

Many greetings, Martin

1 Answers

The result is a stream of records where you can access each record and their fields.

You can create a result stream and iterate over it as following sample code while accessing each field

stream<record{}, error> resultStream =
    mysqlClient->query("Select * from Customers");

error? e = resultStream.forEach(function(record {} result) {
    io:println("Customer full details: ", result);
    io:println("Customer first name: ", result["FirstName"]);
    io:println("Customer last name: ", result["LastName"]);
});

You can refer more JDBC examples in Ballerina By Examples

Related