1
1
2
- <!-- ## Insert is present in the general docs here-->
3
-
2
+ ## Insert
3
+
4
+ Data entry is as easy as providing the appropriate data structure to a permitted table.
5
+ Given the following table definition, we can insert data as tuples, dicts, pandas
6
+ dataframes, or pathlib ` Path ` relative paths to local CSV files.
7
+
8
+ ``` text
9
+ mouse_id: int # unique mouse id
10
+ ---
11
+ dob: date # mouse date of birth
12
+ sex: enum('M', 'F', 'U') # sex of mouse - Male, Female, or Unknown
13
+ ```
14
+
15
+ === "Tuple"
16
+
17
+ ```python
18
+ mouse.insert1( (0, '2017-03-01', 'M') ) # Single entry
19
+ data = [
20
+ (1, '2016-11-19', 'M'),
21
+ (2, '2016-11-20', 'U'),
22
+ (5, '2016-12-25', 'F')
23
+ ]
24
+ mouse.insert(data) # Multi-entry
25
+ ```
26
+
27
+ === "Dict"
28
+
29
+ ```python
30
+ mouse.insert1( dict(mouse_id=0, dob='2017-03-01', sex='M') ) # Single entry
31
+ data = [
32
+ {'mouse_id':1, 'dob':'2016-11-19', 'sex':'M'},
33
+ {'mouse_id':2, 'dob':'2016-11-20', 'sex':'U'},
34
+ {'mouse_id':5, 'dob':'2016-12-25', 'sex':'F'}
35
+ ]
36
+ mouse.insert(data) # Multi-entry
37
+ ```
38
+
39
+ === "Pandas"
40
+
41
+ ```python
42
+ import pandas as pd
43
+ data = pd.DataFrame(
44
+ [[1, "2016-11-19", "M"], [2, "2016-11-20", "U"], [5, "2016-12-25", "F"]],
45
+ columns=["mouse_id", "dob", "sex"],
46
+ )
47
+ mouse.insert(data)
48
+ ```
49
+
50
+ === "CSV"
51
+
52
+ Given the following CSV in the current working directory as `mice.csv`
53
+
54
+ ```console
55
+ mouse_id,dob,sex
56
+ 1,2016-11-19,M
57
+ 2,2016-11-20,U
58
+ 5,2016-12-25,F
59
+ ```
60
+
61
+ We can import as follows:
62
+
63
+ ```python
64
+ from pathlib import Path
65
+ mouse.insert(Path('./mice.csv'))
66
+ ```
67
+
4
68
## Make
5
69
6
70
See the article on [ ` make ` methods] ( ../../reproduce/make-method/ )
@@ -31,8 +95,8 @@ data = query.fetch(as_dict=True) # (2)
31
95
### Separate variables
32
96
33
97
``` python
34
- name, img = query.fetch1(' name ' , ' image ' ) # when query has exactly one entity
35
- name, img = query.fetch(' name ' , ' image ' ) # [name , ...] [image , ...]
98
+ name, img = query.fetch1(' mouse_id ' , ' dob ' ) # when query has exactly one entity
99
+ name, img = query.fetch(' mouse_id ' , ' dob ' ) # [mouse_id , ...] [dob , ...]
36
100
```
37
101
38
102
### Primary key values
@@ -51,19 +115,18 @@ primary keys.
51
115
To sort the result, use the ` order_by ` keyword argument.
52
116
53
117
``` python
54
- data = query.fetch(order_by = ' name' ) # ascending order
55
- data = query.fetch(order_by = ' name desc' ) # descending order
56
- data = query.fetch(order_by = (' name desc' , ' year' )) # by name first, year second
57
- data = query.fetch(order_by = ' KEY' ) # sort by the primary key
58
- data = query.fetch(order_by = (' name' , ' KEY desc' )) # sort by name but for same names order by primary key
118
+ data = query.fetch(order_by = ' mouse_id' ) # ascending order
119
+ data = query.fetch(order_by = ' mouse_id desc' ) # descending order
120
+ data = query.fetch(order_by = (' mouse_id' , ' dob' )) # by ID first, dob second
121
+ data = query.fetch(order_by = ' KEY' ) # sort by the primary key
59
122
```
60
123
61
124
The ` order_by ` argument can be a string specifying the attribute to sort by. By default
62
125
the sort is in ascending order. Use ` 'attr desc' ` to sort in descending order by
63
126
attribute ` attr ` . The value can also be a sequence of strings, in which case, the sort
64
127
performed on all the attributes jointly in the order specified.
65
128
66
- The special attribute name ` 'KEY' ` represents the primary key attributes in order that
129
+ The special attribute named ` 'KEY' ` represents the primary key attributes in order that
67
130
they appear in the index. Otherwise, this name can be used as any other argument.
68
131
69
132
If an attribute happens to be a SQL reserved word, it needs to be enclosed in
@@ -82,7 +145,7 @@ Similar to sorting, the `limit` and `offset` arguments can be used to limit the
82
145
to a subset of entities.
83
146
84
147
``` python
85
- data = query.fetch(order_by = ' name ' , limit = 10 , offset = 5 )
148
+ data = query.fetch(order_by = ' mouse_id ' , limit = 10 , offset = 5 )
86
149
```
87
150
88
151
Note that an ` offset ` cannot be used without specifying a ` limit ` as
0 commit comments