Skip to content

Commit d7a0067

Browse files
committed
Improve quickstart
1 parent fc75b15 commit d7a0067

File tree

1 file changed

+39
-47
lines changed

1 file changed

+39
-47
lines changed

README.md

Lines changed: 39 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -14,74 +14,66 @@ You can use it to:
1414
- Read and write Arrow, Parquet, JSON, and CSV formats natively.
1515
- Create DuckDB extensions in Rust with custom scalar and table functions.
1616

17-
## Quick start
17+
## Quickstart
1818

19-
```rust
20-
use duckdb::{params, Connection, Result};
19+
Create a new project and add the `duckdb` crate:
20+
21+
```shell
22+
cargo new quack-in-rust
23+
cd quack-in-rust
24+
cargo add duckdb -F bundled
25+
```
2126

22-
// In your project, we need to keep the arrow version same as the version used in duckdb.
23-
// Refer to https://github.com/duckdb/duckdb-rs/issues/92
24-
// You can either:
25-
use duckdb::arrow::record_batch::RecordBatch;
26-
// Or in your Cargo.toml, use * as the version; features can be toggled according to your needs
27-
// arrow = { version = "*", default-features = false, features = ["prettyprint"] }
28-
// Then you can:
29-
// use arrow::record_batch::RecordBatch;
27+
Update `src/main.rs` with the following code:
3028

31-
use duckdb::arrow::util::pretty::print_batches;
29+
```rust
30+
use duckdb::{params, Connection, Result};
3231

33-
#[derive(Debug)]
34-
struct Person {
32+
struct Duck {
3533
id: i32,
3634
name: String,
37-
data: Option<Vec<u8>>,
3835
}
3936

4037
fn main() -> Result<()> {
4138
let conn = Connection::open_in_memory()?;
4239

40+
conn.execute(
41+
"CREATE TABLE ducks (id INTEGER PRIMARY KEY, name TEXT)",
42+
[], // empty list of parameters
43+
)?;
44+
4345
conn.execute_batch(
44-
r"CREATE SEQUENCE seq;
45-
CREATE TABLE person (
46-
id INTEGER PRIMARY KEY DEFAULT NEXTVAL('seq'),
47-
name TEXT NOT NULL,
48-
data BLOB
49-
);
50-
")?;
51-
52-
let me = Person {
53-
id: 0,
54-
name: "Steven".to_string(),
55-
data: None,
56-
};
46+
r#"
47+
INSERT INTO ducks (id, name) VALUES (1, 'Donald Duck');
48+
INSERT INTO ducks (id, name) VALUES (2, 'Scrooge McDuck');
49+
"#,
50+
)?;
51+
5752
conn.execute(
58-
"INSERT INTO person (name, data) VALUES (?, ?)",
59-
params![me.name, me.data],
53+
"INSERT INTO ducks (id, name) VALUES (?, ?)",
54+
params![3, "Darkwing Duck"],
6055
)?;
6156

62-
// query table by rows
63-
let mut stmt = conn.prepare("SELECT id, name, data FROM person")?;
64-
let person_iter = stmt.query_map([], |row| {
65-
Ok(Person {
66-
id: row.get(0)?,
67-
name: row.get(1)?,
68-
data: row.get(2)?,
69-
})
70-
})?;
71-
72-
for person in person_iter {
73-
let p = person.unwrap();
74-
println!("ID: {}", p.id);
75-
println!("Found person {:?}", p);
57+
let ducks = conn
58+
.prepare("FROM ducks")?
59+
.query_map([], |row| {
60+
Ok(Duck {
61+
id: row.get(0)?,
62+
name: row.get(1)?,
63+
})
64+
})?
65+
.collect::<Result<Vec<_>>>()?;
66+
67+
for duck in ducks {
68+
println!("{}) {}", duck.id, duck.name);
7669
}
7770

78-
// query table by arrow
79-
let rbs: Vec<RecordBatch> = stmt.query_arrow([])?.collect();
80-
print_batches(&rbs).unwrap();
8171
Ok(())
8272
}
8373
```
8474

75+
Execute the program with `cargo run` and watch DuckDB in action!
76+
8577
## Examples
8678

8779
The following [examples](crates/duckdb/examples) demonstrate various features and use cases of duckdb-rs:

0 commit comments

Comments
 (0)