Skip to content

Command-line tool to convert Apple HealthKit data to a SQLite database.

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

jshrake/healthkit-to-sqlite

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

healthkit-to-sqlite

ci crates.io Apache 2.0 License: MIT

Command-line tool to convert Apple HealthKit data to a SQLite database.

Getting Started

  1. Open the Health app on your iOS device.
  2. Click your profile icon in the top-right corner.
  3. Click the "Export All Health Data" button.
  4. Share the resulting ZIP archive to your computer.
  5. Run healthkit-to-sqlite on the exported ZIP archive.
# You need to install Rust https://rustup.rs/
cargo install healthkit-to-sqlite-cli
healthkit-to-sqlite export.zip sqlite://healthkit.db

Please create an issue for all bugs, feature requests, or feedback.

Example Queries

Here are a few example SQL queries to help you start exploring your HealthKit data:

  • Total walking, running, and hiking workout duration in hours for the month of December 2022:
select
  sum(duration) / 60 as total_duration
from
  workout
where
  (
    creationDate between '2022-12-01' and '2022-12-31'
  )
  and (
    workoutActivityType = 'HKWorkoutActivityTypeWalking' or
    workoutActivityType = 'HKWorkoutActivityTypeRunning' or
    workoutActivityType = 'HKWorkoutActivityTypeHiking'
  );
  • Total distance covered in miles across all workouts for the month of December 2022:
select
  sum(
    json_extract(
      workoutStatistics,
      "$.HKQuantityTypeIdentifierDistanceWalkingRunning.sum"
    )
  ) as total_distance_miles
from
  workout
where
  (
    creationDate between '2022-12-01'
    and '2022-12-31'
  );
  • The JSON data in the workoutStatistics column looks like:
{
    "HKQuantityTypeIdentifierActiveEnergyBurned": {
        "endDate": "2019-12-27 13:10:51 -0800",
        "startDate": "2019-12-27 12:30:15 -0800",
        "sum": 135.70199584960938,
        "type": "HKQuantityTypeIdentifierActiveEnergyBurned",
        "unit": "Cal"
    },
    "HKQuantityTypeIdentifierBasalEnergyBurned": {
        "endDate": "2019-12-27 13:10:51 -0800",
        "startDate": "2019-12-27 12:30:15 -0800",
        "sum": 67.24250030517578,
        "type": "HKQuantityTypeIdentifierBasalEnergyBurned",
        "unit": "Cal"
    },
    "HKQuantityTypeIdentifierDistanceWalkingRunning": {
        "endDate": "2019-12-27 13:10:51 -0800",
        "startDate": "2019-12-27 12:30:15 -0800",
        "sum": 1.4269200563430786,
        "type": "HKQuantityTypeIdentifierDistanceWalkingRunning",
        "unit": "mi"
    }
}

Datasette

You can use https://datasette.io/ to view and explore the resulting SQLite database file.

datasette healthkit.db
datasette install datasette-geojson-map
datasette install datasette-leaflet-geojson

Decisions

  • Only the Record, Workout, and ActivitySummary elements are currently exported.
  • Record elements are inserted to a table with a name matching the value of the element's type attribute.
  • Workout elements are inserted to a table named "Workout".
    • The descendent workoutEvent and workoutStatistics elements are represented as JSON columns.
    • The descendent workoutRoute element is converted to a GeoJSON LineString and stored in a JSON column named "geometry" for easy integration with https://datasette.io/plugins/datasette-geojson-map.
  • ActivitySummary elements are inserted as rows to a table named "ActivitySummary".
  • In an attempt to future proof against Apple adding, removing, or changing element attributes, the code only assumes the existence of a limited number of attributes:
    • Record elements must have a type attribute.
    • Workout elements must have a workoutActivity attribute.
    • MetadataEntry elements must have key and value attributes.
    • FileReference elements must have a path attribute.

License

This project is licensed under either of

at your option.

About

Command-line tool to convert Apple HealthKit data to a SQLite database.

Topics

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Packages

No packages published

Languages