This time we want to get the data from the database directly. We will use the sqljocky package to do this. First of all we create a new console dart application. After that we need to get the package from the pub.
- Create a
pubspec.yaml
file in the project and add the following text:name: my_app dependencies:
web_ui: any
- Rightclick on the
pubspec.yaml
file and choose Tools > Pub Install.
- Now you can see a new package in the packages folder:
In the .dart file we now need to add the import to the package:
import 'package:sqljocky/sqljocky.dart';
Now we can access this package. The following code is doing the whole action:
void main() { var con = new ConnectionPool(host: 'localhost', port: 3306, user: 'root', password: null, db: 'mymusic', max: 5); con.query('select albumName, interpret from cd').then((result) { var row; for (row in result) { print('album: ${row[0]} (${row[1]})'); } } ); }
The code is real simple. The library is really doing a lot for us. To read from the result of the select-statement we can use a simple foreach snippet:
var row; for (row in result) { print('album: ${row[0]} (${row[1]})'); }
The output in the console looks like that:
This are the things I really love about dart. Just a few easy lines of code for a great result.
One thought on “Dart: sqljocky example”