Fixed up example
This commit is contained in:
parent
e2260ca15b
commit
df9a348980
23
README.md
23
README.md
@ -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);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
Loading…
Reference in New Issue
Block a user