@@ -14,74 +14,66 @@ You can use it to:
14
14
- Read and write Arrow, Parquet, JSON, and CSV formats natively.
15
15
- Create DuckDB extensions in Rust with custom scalar and table functions.
16
16
17
- ## Quick start
17
+ ## Quickstart
18
18
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
+ ```
21
26
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:
30
28
31
- use duckdb :: arrow :: util :: pretty :: print_batches;
29
+ ``` rust
30
+ use duckdb :: {params, Connection , Result };
32
31
33
- #[derive(Debug )]
34
- struct Person {
32
+ struct Duck {
35
33
id : i32 ,
36
34
name : String ,
37
- data : Option <Vec <u8 >>,
38
35
}
39
36
40
37
fn main () -> Result <()> {
41
38
let conn = Connection :: open_in_memory ()? ;
42
39
40
+ conn . execute (
41
+ " CREATE TABLE ducks (id INTEGER PRIMARY KEY, name TEXT)" ,
42
+ [], // empty list of parameters
43
+ )? ;
44
+
43
45
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
+
57
52
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 " ],
60
55
)? ;
61
56
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);
76
69
}
77
70
78
- // query table by arrow
79
- let rbs : Vec <RecordBatch > = stmt . query_arrow ([])? . collect ();
80
- print_batches (& rbs ). unwrap ();
81
71
Ok (())
82
72
}
83
73
```
84
74
75
+ Execute the program with ` cargo run ` and watch DuckDB in action!
76
+
85
77
## Examples
86
78
87
79
The following [ examples] ( crates/duckdb/examples ) demonstrate various features and use cases of duckdb-rs:
0 commit comments