Java Reference
In-Depth Information
The execute method takes a SQL string and runs it against the database. Here again, I'm
taking advantage of the optional parentheses.
The next step is to create the table to hold the stadium information:
db.execute '''
create table stadium(
id int not null auto_increment,
name varchar(200) not null,
city varchar(200) not null,
state char(2) not null,
team char(3) not null,
latitude double,
longitude double,
primary key(id)
);
'''
The three single quotes represent a multiline string in Groovy. Three double quotes would
be a multiline GString , which I could use for parameter substitution, but they're not
needed in this particular case.
Now that the table has been constructed it's time to populate the table with stadium data:
Geocoder geo = new Geocoder()
stadiums. each { s ->
geo.fillInLatLng s
db.execute """
insert into stadium(name, city, state, team, latitude, longitude)
values(${s.name},${s.city},${s.state},
${s.team},${s.latitude},${s.longitude});
"""
}
After instantiating the geocoder I walk through each stadium in the collection, assigning
each to the dummy variable s . For each one, after computing the latitude and longitude, I
execute an insert statement contained within three double quotes, where I substitute the
values I need from the stadium using the standard ${...} notation.
All that remains is to do some kind of sanity check to make sure that the values received
are reasonable. Here are some assert statements to do just that:
Search WWH ::




Custom Search