#Count DB rows in stream

1 messages · Page 1 of 1 (latest)

arctic dawn
#

Hello everyone!

I'm working with a service that queries a database.
The query response is executed as stream<model, error?> dbResult = connection -> query (SELECT NAME, AGE FROM PERSON);

When I run dbResult.count(), it returns 1 instead of 0 when it doesn't find any records. How can I get it to return the number of rows?

I'm working with version 2201.10.3

bright valve
#

You can use the following code snippet to achieve your task:

int count = check from model i in dbResult collect count(i);

Note:
When iterating through the stream, the elements will be consumed. Therefore, if you need to perform additional operations on the stream elements, you should include that logic within the same iteration.

#

In the other hand, the count method is used only for return the number of arguments.

example

int count = value:count([1, 5, 3, 4, 2]);
io:println(count); // prints 1

count = [1, 5, 3, 4, 2].count();
io:println(count); // prints 1

count = value:count(1, 2, 3, 4, 5);
io:println(count); // prints 5