Skip to content

Instantly share code, notes, and snippets.

@PedersenThomas
Last active December 23, 2015 15:59
Show Gist options
  • Select an option

  • Save PedersenThomas/6659049 to your computer and use it in GitHub Desktop.

Select an option

Save PedersenThomas/6659049 to your computer and use it in GitHub Desktop.

Revisions

  1. PedersenThomas revised this gist Sep 22, 2013. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions gistfile1.dart
    Original file line number Diff line number Diff line change
    @@ -27,8 +27,9 @@ Future insertPerson(Connection connection,
    DateTime dateOfBirth, double height) {
    Completer _completer = new Completer();

    final String query = "INSERT INTO person (firstname, lastname, dateofbirth, height) VALUES" +
    "('$firstname', '$lastname', '$dateOfBirth', $height);";
    final String query =
    "INSERT INTO person (firstname, lastname, dateofbirth, height) VALUES" +
    "('$firstname', '$lastname', '$dateOfBirth', $height);";
    connection.execute(query).then((rowsAffected) {
    print("Rows Affected: $rowsAffected");
    _completer.complete();
  2. PedersenThomas revised this gist Sep 22, 2013. 1 changed file with 6 additions and 3 deletions.
    9 changes: 6 additions & 3 deletions gistfile1.dart
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@ void main() {
    connect(uri).then((Connection connection) {

    //Insert a new person
    insertPerson(connection, "Thomas", "Pedersen", new DateTime(1990, 01, 1), 1.92).then((_){
    insertPerson(connection, "Thomas", "Pedersen", new DateTime(1990, 01, 1), 1.92).then((_) {

    //Print out the table.
    printEntireTable(connection).then((_) {
    @@ -22,10 +22,13 @@ void main() {
    });
    }

    Future insertPerson(Connection connection, String firstname, String lastname, DateTime dateOfBirth, double height) {
    Future insertPerson(Connection connection,
    String firstname, String lastname,
    DateTime dateOfBirth, double height) {
    Completer _completer = new Completer();

    final String query = "INSERT INTO person (firstname, lastname, dateofbirth, height) VALUES ('$firstname', '$lastname', '$dateOfBirth', $height);";
    final String query = "INSERT INTO person (firstname, lastname, dateofbirth, height) VALUES" +
    "('$firstname', '$lastname', '$dateOfBirth', $height);";
    connection.execute(query).then((rowsAffected) {
    print("Rows Affected: $rowsAffected");
    _completer.complete();
  3. PedersenThomas created this gist Sep 22, 2013.
    50 changes: 50 additions & 0 deletions gistfile1.dart
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    import 'dart:async';

    import 'package:postgresql/postgresql.dart';

    void main() {
    var username = "TheRightMan";
    var password = "WithTheRightSecret";
    var DBname = "AtTheRightPlace";
    var uri = 'postgres://$username:$password@localhost:5432/$DBname';

    //Opens a connection.
    connect(uri).then((Connection connection) {

    //Insert a new person
    insertPerson(connection, "Thomas", "Pedersen", new DateTime(1990, 01, 1), 1.92).then((_){

    //Print out the table.
    printEntireTable(connection).then((_) {
    connection.close();
    });
    });
    });
    }

    Future insertPerson(Connection connection, String firstname, String lastname, DateTime dateOfBirth, double height) {
    Completer _completer = new Completer();

    final String query = "INSERT INTO person (firstname, lastname, dateofbirth, height) VALUES ('$firstname', '$lastname', '$dateOfBirth', $height);";
    connection.execute(query).then((rowsAffected) {
    print("Rows Affected: $rowsAffected");
    _completer.complete();
    }).catchError((error) => _completer.completeError(error));

    return _completer.future;
    }

    Future printEntireTable(Connection connection) {
    Completer _completer = new Completer();

    final String query = "SELECT id, firstname, lastname, dateofbirth, height FROM person";
    connection.query(query).toList().then((rows) {
    for(var row in rows) {
    var age = ((new DateTime.now()).difference(row.dateofbirth).inDays/365.2425).floor();
    print("(${row.id}) ${row.firstname} ${row.lastname} - $age years old - ${row.height}m");
    }
    _completer.complete();
    }).catchError((error) => _completer.completeError(error));

    return _completer.future;
    }