@@ -6,126 +6,157 @@ package main
6
6
7
7
import (
8
8
"bufio"
9
+ "database/sql"
9
10
"fmt"
11
+ "log"
10
12
"os"
11
13
"strconv"
12
14
"strings"
15
+
16
+ _ "github.com/mattn/go-sqlite3"
13
17
)
14
18
15
19
type Obs struct {
16
- GlobalUniqueIdentifier string
17
- Last_edited_date string
18
- Taxonomic_order string
19
- Category string
20
- Common_name string
21
- Scientific_name string
22
- Subspecies_common_name string
23
- Subspecies_scientific_name string
24
- Observation_count string
25
- Breeding_bird_atlas_code string
26
- Breeding_bird_atlas_category string
27
- AgeSex string
28
- Country string
29
- Country_code string
30
- State string
31
- State_code string
32
- County string
33
- County_code string
34
- Iba_code string
35
- Bcr_code string
36
- Usfws_code string
37
- Atlas_block string
38
- Locality string
39
- Locality_id string
40
- Locality_type string
41
- Latitude float64
42
- Longitude float64
43
- Observation_date string
44
- Time_observations_started string
45
- Observer_id string
46
- Sampling_event_identifier string
47
- Protocol_type string
48
- Protocol_code string
49
- Project_code string
50
- Duration_minutes int
51
- Effort_distance_km float64
52
- Effort_area_ha float64
53
- Number_observers int
54
- All_species_reported bool
55
- Group_identifier string
56
- Has_media bool
57
- Approved bool
58
- Reviewed bool
59
- Reason string
60
- Trip_comments string
61
- Species_comments string
20
+ GlobalUniqueIdentifier string
21
+ LastEditedDate string
22
+ TaxonomicOrder string
23
+ Category string
24
+ CommonName string
25
+ ScientificName string
26
+ SubspeciesCommonName string
27
+ SubspeciesScientificName string
28
+ ObservationCount int
29
+ BreedingBirdAtlasCode string
30
+ BreedingBirdAtlasCategory string
31
+ AgeSex string
32
+ Country string
33
+ CountryCode string
34
+ State string
35
+ StateCode string
36
+ County string
37
+ CountyCode string
38
+ IBACode string
39
+ BCRCode string
40
+ USFWSCode string
41
+ AtlasBlock string
42
+ Locality string
43
+ LocalityID string
44
+ LocalityType string
45
+ Latitude float64
46
+ Longitude float64
47
+ ObservationDate string
48
+ TimeObservationsStarted string
49
+ ObserverID string
50
+ SamplingEventIdentifier string
51
+ ProtocolType string
52
+ ProtocolCode string
53
+ ProjectCode string
54
+ DurationMinutes int
55
+ EffortDistanceKM float64
56
+ EffortAreaHA float64
57
+ NumberObservers int
58
+ AllSpeciesReported bool
59
+ GroupIdentifier string
60
+ HasMedia bool
61
+ Approved bool
62
+ Reviewed bool
63
+ Reason string
64
+ TripComments string
65
+ SpeciesComments string
62
66
}
63
67
64
68
func decodeObs (vals []string ) (Obs , error ) {
65
69
var birds Obs
66
70
var err error
67
- if len (vals ) != 43 {
68
- return birds , fmt .Errorf ("bad values, got:%d, want: 33 " , len (vals ))
71
+ if len (vals ) != 47 {
72
+ return birds , fmt .Errorf ("bad values, got:%d, want: 46 " , len (vals ))
69
73
}
70
74
birds .GlobalUniqueIdentifier = vals [0 ]
71
- birds .Last_edited_date = vals [1 ]
72
- birds .Taxonomic_order = vals [2 ]
75
+ birds .LastEditedDate = vals [1 ]
76
+ birds .TaxonomicOrder = vals [2 ]
73
77
birds .Category = vals [3 ]
74
- birds .Common_name = vals [4 ]
75
- birds .Scientific_name = vals [5 ]
76
- birds .Subspecies_common_name = vals [6 ]
77
- birds .Subspecies_scientific_name = vals [7 ]
78
- birds .Observation_count = vals [8 ]
79
- birds .Breeding_bird_atlas_code = vals [9 ]
80
- birds .Breeding_bird_atlas_category = vals [10 ]
78
+ birds .CommonName = vals [4 ]
79
+ birds .ScientificName = vals [5 ]
80
+ birds .SubspeciesCommonName = vals [6 ]
81
+ birds .SubspeciesScientificName = vals [7 ]
82
+ if vals [8 ] == "X" {
83
+ birds .ObservationCount = - 1
84
+ } else {
85
+ birds .ObservationCount , err = strconv .Atoi (vals [8 ])
86
+ if err != nil {
87
+ return birds , err
88
+ }
89
+ }
90
+ birds .BreedingBirdAtlasCode = vals [9 ]
91
+ birds .BreedingBirdAtlasCategory = vals [10 ]
81
92
birds .AgeSex = vals [11 ]
82
93
birds .Country = vals [12 ]
83
- birds .Country_code = vals [13 ]
94
+ birds .CountryCode = vals [13 ]
84
95
birds .State = vals [14 ]
85
- birds .State_code = vals [15 ]
96
+ birds .StateCode = vals [15 ]
86
97
birds .County = vals [16 ]
87
- birds .County_code = vals [17 ]
88
- birds .Iba_code = vals [18 ]
89
- birds .Bcr_code = vals [19 ]
90
- birds .Usfws_code = vals [20 ]
91
- birds .Atlas_block = vals [21 ]
98
+ birds .CountyCode = vals [17 ]
99
+ birds .IBACode = vals [18 ]
100
+ birds .BCRCode = vals [19 ]
101
+ birds .USFWSCode = vals [20 ]
102
+ birds .AtlasBlock = vals [21 ]
92
103
birds .Locality = vals [22 ]
93
- birds .Locality_id = vals [23 ]
94
- birds .Locality_type = vals [24 ]
95
- birds .Latitude , err = strconv .ParseFloat (vals [25 ], 64 )
96
- if err != nil {
97
- return birds , err
104
+ birds .LocalityID = vals [23 ]
105
+ birds .LocalityType = vals [24 ]
106
+ if vals [25 ] != "" {
107
+ birds .Latitude , err = strconv .ParseFloat (vals [25 ], 64 )
108
+ if err != nil {
109
+ return birds , err
110
+ }
98
111
}
99
- /*
100
- birds.Longitude, err = strconv.ParseFloat(vals[24 ], 64)
112
+ if vals [ 26 ] != "" {
113
+ birds .Longitude , err = strconv .ParseFloat (vals [26 ], 64 )
101
114
if err != nil {
115
+ fmt .Println (vals [21 :27 ])
102
116
return birds , err
103
117
}
104
- birds.Observation_date = vals[25]
105
- birds.Time_observations_started = vals[26]
106
- birds.Observer_id = vals[27]
107
- birds.Sampling_event_identifier = vals[28]
108
- birds.Protocol_type = vals[29]
109
- birds.Protocol_code = vals[30]
110
- birds.Project_code = vals[31]
111
- birds.Duration_minutes, err = strconv.Atoi(vals[32])
118
+ }
119
+ birds .ObservationDate = vals [27 ]
120
+ birds .TimeObservationsStarted = vals [28 ]
121
+ birds .ObserverID = vals [29 ]
122
+ birds .SamplingEventIdentifier = vals [30 ]
123
+ birds .ProtocolType = vals [31 ]
124
+ birds .ProtocolCode = vals [32 ]
125
+ birds .ProjectCode = vals [33 ]
126
+ if vals [34 ] != "" {
127
+ birds .DurationMinutes , err = strconv .Atoi (vals [34 ])
112
128
if err != nil {
113
129
return birds , err
114
130
}
115
- Effort_distance_km float64
116
- Effort_area_ha float64
117
- Number_observers int
118
- All_species_reported bool
119
- Group_identifier string
120
- Has_media bool
121
- Approved bool
122
- Reviewed bool
123
- Reason string
124
- Trip_comments string
125
- Species_comments string
126
- */
127
- return birds , nil
131
+ }
128
132
133
+ if vals [35 ] != "" {
134
+ birds .EffortDistanceKM , err = strconv .ParseFloat (vals [35 ], 64 )
135
+ if err != nil {
136
+ return birds , err
137
+ }
138
+ }
139
+ if vals [36 ] != "" {
140
+ birds .EffortAreaHA , err = strconv .ParseFloat (vals [36 ], 64 )
141
+ if err != nil {
142
+ return birds , err
143
+ }
144
+ }
145
+ if vals [37 ] != "" {
146
+ birds .NumberObservers , err = strconv .Atoi (vals [37 ])
147
+ if err != nil {
148
+ return birds , err
149
+ }
150
+ }
151
+ birds .AllSpeciesReported = vals [38 ] == "1"
152
+ birds .GroupIdentifier = vals [39 ]
153
+ birds .HasMedia = vals [40 ] == "1"
154
+ birds .Approved = vals [41 ] == "1"
155
+ birds .Reviewed = vals [42 ] == "1"
156
+ birds .Reason = vals [43 ]
157
+ birds .TripComments = vals [44 ]
158
+ birds .SpeciesComments = vals [45 ]
159
+ return birds , nil
129
160
}
130
161
131
162
func help () {
@@ -152,20 +183,55 @@ func main() {
152
183
help ()
153
184
os .Exit (1 )
154
185
}
186
+
187
+ var db * sql.DB
188
+ db , err = sql .Open ("sqlite3" , "ebird.db" )
189
+ if err != nil {
190
+ log .Fatal (err )
191
+ }
192
+
193
+ var sql string
194
+ sql = `CREATE TABLE IF NOT EXISTS test_table (
195
+ first_name TEXT,
196
+ last_name TEXT,
197
+ age INTEGER,
198
+ height FLOAT
199
+ )`
200
+ _ , err = db .Exec (sql )
201
+ if err != nil {
202
+ log .Fatal (err )
203
+ }
204
+
205
+ sql = `INSERT INTO test_table(first_name, last_name, age, height)
206
+ VALUES(?,?,?,?)`
207
+ stmt , err := db .Prepare (sql )
208
+ if err != nil {
209
+ log .Fatal (err )
210
+ }
211
+ _ , err = stmt .Exec ("kyle" , "shannon" , 39 , 5.75 )
212
+ if err != nil {
213
+ log .Fatal (err )
214
+ }
215
+ stmt .Close ()
216
+
155
217
var scn * bufio.Scanner
156
218
scn = bufio .NewScanner (fin )
157
219
var hasRow bool
158
220
var values []string
159
221
hasRow = scn .Scan ()
160
- var nextObs Obs
222
+ // var nextObs Obs
161
223
for hasRow == true {
162
224
hasRow = scn .Scan ()
163
225
if hasRow == false {
164
226
break
165
227
}
166
228
values = strings .Split (scn .Text (), "\t " )
167
- _ = values
168
-
229
+ _ , err := decodeObs (values )
230
+ if err != nil {
231
+ for i , v := range values {
232
+ fmt .Printf ("%d -> %s\n " , i , v )
233
+ }
234
+ log .Fatal (err , values )
235
+ }
169
236
}
170
-
171
237
}
0 commit comments