O'Reilly logo

Baseball Hacks by Joseph Adler

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Keep Your Stats Database Up-to-Date

Create your own database of player statistics and keep it up-to-date as the season progresses.

If you want to know how a player is doing this season, the easiest way to learn is by looking it up at a popular baseball web site [Hack #5] . Most of the time, this will be fine: for example, if you just want to know David Ortiz’s slugging percentage for the year. However, suppose you want to ask a complicated question (say, how did David Ortiz do in the first two weeks of May?). Or suppose you want to use the data for something else, like managing your fantasy team (or league). In these cases, you might want to keep your own up-to-date database.

This hack shows a method for doing this.

The Code

Here’s the code to fetch data from the Web and save it directly to a database. There are three parts to this code: a script to create a database, a script to fill the database initially, and a script to update the database.

Create the box score database.

Let’s start off by creating a simple database in MySQL. If you want to follow along, you can create a file called create_boxes_db.sql and enter all the code in this section into the file:

 -- create a new database schema for the box score information, -- and a user to access the database GRANT ALL ON boxes.* to 'boxer'@'localhost' IDENTIFIED BY 'boxers password'; CREATE DATABASE boxes; USE boxes; -- Create three tables containing information about players: -- batting, fielding, and pitching. Fields are sized to ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required