Skip to content

Instantly share code, notes, and snippets.

@ccjr
Last active March 31, 2016 03:45
Show Gist options
  • Save ccjr/fc47ea0c855b325b3d21b06dcfe9bff3 to your computer and use it in GitHub Desktop.
Save ccjr/fc47ea0c855b325b3d21b06dcfe9bff3 to your computer and use it in GitHub Desktop.
// postgres = "0.11.5"
extern crate postgres;
use postgres::{Connection, SslMode};
fn main() {
let conn = Connection::connect("postgres://foo:bar@localhost/playground", SslMode::None)
.unwrap();
setup_db(&conn);
let sort = "id DESC";
for row in &conn.query("SELECT id FROM foo ORDER BY $1", &[&sort]).unwrap() {
let id: i32 = row.get("id");
println!("** ORDER BY {}: {}", &sort, id);
}
}
fn setup_db(conn: &Connection) {
let _ = conn.execute("CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY)", &[]);
let _ = conn.execute("INSERT INTO foo VALUES (1)", &[]);
let _ = conn.execute("INSERT INTO foo VALUES (2)", &[]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment