Skip to content

Instantly share code, notes, and snippets.

@justinfagnani
Forked from PedersenThomas/gist:6659049
Last active December 23, 2015 18:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinfagnani/6675416 to your computer and use it in GitHub Desktop.
Save justinfagnani/6675416 to your computer and use it in GitHub Desktop.
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) =>
insertPerson(connection, "Thomas", "Pedersen", new DateTime(1990, 01, 1), 1.92))
.then((connection) =>
printEntireTable(connection))
.then((connection) =>
connection.close());
}
Future insertPerson(Connection connection,
String firstname, String lastname,
DateTime dateOfBirth, double height) {
final String query =
"INSERT INTO person (firstname, lastname, dateofbirth, height) VALUES" +
"('$firstname', '$lastname', '$dateOfBirth', $height);";
return connection.execute(query).then((rowsAffected) {
print("Rows Affected: $rowsAffected");
});
}
Future printEntireTable(Connection connection) {
final String query = "SELECT id, firstname, lastname, dateofbirth, height FROM person";
return connection.query(query).listen((row) {
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");
}).asFuture();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment