Dart: sqljocky example

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.

  1. Create a pubspec.yaml file in the project and add the following text:
     name: my_app
     dependencies:
      web_ui: any
  2. Rightclick on the pubspec.yaml file and choose Tools > Pub Install.
    Screen Shot 2013-01-27 at 12.44.10
  3. Now you can see a new package in the packages folder:
    Screen Shot 2013-01-27 at 12.50.26

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:

Screen Shot 2013-01-27 at 13.10.04

This are the things I really love about dart. Just a few easy lines of code for a great result.

Yannick Signer

 

One thought on “Dart: sqljocky example
ambusy123

Created a new Web-application. Edited the YAML file:

name:ArCat

description: A sample web application

dependencies:

browser: any

web_ui: any

rightclick on yaml file gives me
Pub Get
Pub Get offline
Pub Build
Pub Upgrade.

Missing: install!.
and , by the way, where do I specify that I want SqlJocky installed?

Txs

Leave a Reply

Your email address will not be published. Required fields are marked *