Back to Examples
- Binding patterns
- Typed binding pattern
- Wildcard binding pattern
- List binding patterns
- Rest binding pattern in list binding pattern
- Mapping binding pattern
- Rest binding pattern in mapping binding pattern
- Error binding pattern
- Rest binding pattern in error binding pattern
- Single use of typed binding patterns
- Single use of typed binding patterns with on fail clause
- Iterative use of typed binding patterns
- List binding pattern in match statement
- Mapping binding pattern in match statement
- Error binding pattern in match statement
- Query expressions
- Sort iterable objects
- Let clause
- Limit clause
- Join iterable objects
- Outer Join clause
- Query tables
- Create tables with a query
- Create maps with a query
- Create streams with a query
- On conflict clause
- Advanced conflict handling
- Iterate over XML with a query
- Nested query expressions
- Destructure records using a query
- Querying streams
- Aggregation
- JSON type
- Access JSON elements
- Access optional JSON elements
- Match statement with maps
- Convert from user-defined type to JSON
- Convert from table and XML to JSON
- Convert from JSON to user-defined type
- Cast JSON to user-defined type
- Resource method typing
- JSON numbers
- JSON to record
- JSON to record with projection
- JSONPath expressions
- Asynchronous function calls
- Named workers
- Sequence diagrams
- Wait for workers
- Strands
- Named worker return values
- Alternate wait
- Multiple wait
- Named workers and futures
- Inter-worker message passing
- Alternate receive
- Multiple receive
- Conditional send
- Inter-worker failure propagation
- Named worker with on fail clause
- Synchronize message passing
- Asynchronize message passing
- Flush
- Fork
Database Access - Simple query
The mysql:Client
allows querying the database with the use of query
method. This method requires a sql:ParameterizedQuery
-typed SQL statement as the argument.
Tip: Checkout
ballerinax/mssql
,ballerinax/postgresql
,ballerinax/oracledb
,ballerinax/java.jdbc
for other supported database clients.
import ballerina/http;
import ballerina/sql;
import ballerinax/mysql;
import ballerinax/mysql.driver as _;
// The `Album` record to load records from `albums` table.
type Album record {|
string id;
string title;
string artist;
float price;
|};
service / on new http:Listener(8080) {
private final mysql:Client db;
function init() returns error? {
// Initiate the mysql client at the start of the service. This will be used
// throughout the lifetime of the service.
self.db = check new ("localhost", "root", "Test@123", "MUSIC_STORE", 3306);
}
resource function get albums() returns Album[]|error {
// Execute simple query to retrieve all records from the `albums` table.
stream<Album, sql:Error?> albumStream = self.db->query(`SELECT * FROM albums`);
// Process the stream and convert results to Album[] or return error.
return from Album album in albumStream
select album;
}
}
Prerequisites
- • To set up the database, see the Database Access Ballerina By Example - Prerequisites.
Run the service.
$ bal run mysql_simple_query.bal
Invoke the service by executing the following cURL command in a new terminal.
$ curl http://localhost:8080/albums[{"id":"A-123", "title":"Lemonade", "artist":"Beyonce", "price":18.98}, {"id":"A-321", "title":"Renaissance", "artist":"Beyonce", "price":24.98}]
Related links
PreviousSend file
NextQuery with one result