Fixed up example

This commit is contained in:
Steven Fackler 2013-10-01 00:01:54 -07:00
parent e2260ca15b
commit df9a348980
2 changed files with 32 additions and 26 deletions

View File

@ -11,15 +11,18 @@ exposes a high level interface in the vein of JDBC or Go's `database/sql`
package.
```rust
extern mod postgres;
extern mod extra;
use postgres::PostgresConnection;
use extra::time;
use extra::time::Timespec;
use postgres::{PostgresConnection, PostgresStatement};
use postgres::types::ToSql;
#[deriving(ToStr)]
struct Person {
id: i32,
name: ~str,
awesome: bool,
time_created: Timespec,
data: Option<~[u8]>
}
@ -29,29 +32,29 @@ fn main() {
conn.update("CREATE TABLE person (
id SERIAL PRIMARY KEY,
name VARCHAR NOT NULL,
awesome BOOL NOT NULL,
time_created TIMESTAMP NOT NULL,
data BYTEA
)", []);
let me = Person {
id: 0,
name: ~"Steven",
awesome: true,
time_created: time::get_time(),
data: None
};
conn.update("INSERT INTO person (name, awesome, data)
conn.update("INSERT INTO person (name, time_created, data)
VALUES ($1, $2, $3)",
[&me.name as &ToSql, &me.awesome as &ToSql,
[&me.name as &ToSql, &me.time_created as &ToSql,
&me.data as &ToSql]);
let stmt = conn.prepare("SELECT id, name, awesome, data FROM person");
let stmt = conn.prepare("SELECT id, name, time_created, data FROM person");
for row in stmt.query([]) {
let person = Person {
id: row[0],
name: row[1],
awesome: row[2],
time_created: row[2],
data: row[3]
};
println!("Found person {}", person.to_str());
println!("Found person {}", person.name);
}
}
```

View File

@ -5,15 +5,18 @@ package.
```rust
extern mod postgres;
extern mod extra;
use postgres::PostgresConnection;
use extra::time;
use extra::time::Timespec;
use postgres::{PostgresConnection, PostgresStatement};
use postgres::types::ToSql;
#[deriving(ToStr)]
struct Person {
id: i32,
name: ~str,
awesome: bool,
time_created: Timespec,
data: Option<~[u8]>
}
@ -23,29 +26,29 @@ fn main() {
conn.update("CREATE TABLE person (
id SERIAL PRIMARY KEY,
name VARCHAR NOT NULL,
awesome BOOL NOT NULL,
time_created TIMESTAMP NOT NULL,
data BYTEA
)", []);
let me = Person {
id: 0,
name: ~"Steven",
awesome: true,
time_created: time::get_time(),
data: None
};
conn.update("INSERT INTO person (name, awesome, data)
conn.update("INSERT INTO person (name, time_created, data)
VALUES ($1, $2, $3)",
[&me.name as &ToSql, &me.awesome as &ToSql,
[&me.name as &ToSql, &me.time_created as &ToSql,
&me.data as &ToSql]);
let stmt = conn.prepare("SELECT id, name, awesome, data FROM person");
let stmt = conn.prepare("SELECT id, name, time_created, data FROM person");
for row in stmt.query([]) {
let person = Person {
id: row[0],
name: row[1],
awesome: row[2],
time_created: row[2],
data: row[3]
};
println!("Found person {}", person.to_str());
println!("Found person {}", person.name);
}
}
```