Core Data and Database Browser for SQLite

OK, so you’ve started to work with Core Data and the persistent store coordinator. It’s cool, but wouldn’t it be nice if you could view the contents of your SQLite database outside of the simulator?

In a few short minutes, you can!

Step One: Install Database Browser for SQLite

Download and install Database Browser for SQLite.

Step Two: Dude, where’s my database?

A little bit of prep work from the command line will save you time later.

When you use the simulator to test your app, all of the sandboxed files are written to the device directory. The filename will be your AppName.sqlite.

The path to the SQLite database file will look like this:

/Users/kirby/Library/Developer/CoreSimulator/Devices/402FCD3E-7A98-4B5A-9CB1-83B1A3F85B47/data/Containers/Data/Application/AB4D98C4-B86A-4FE3-B557-CFC60C83893E/Documents/App.sqlite

Not something that you want to try to remember or waste your time hunting for as you install and reinstall apps.

You guessed it—the directory names are intentionally unpredictable for security purposes.

Symbolic Link

To make it easier to get to the root of Devices, use the ln command from the Terminal command prompt.

First, cd into your home directory, and then create a symbolic link between the real Devices directory and a name of your choosing, e.g. simDevices.

$ cd ~
$ ln -s /Users/kirby/Library/Developer/CoreSimulator/Devices simDevices
$ ls -l simDevices
lrwxr-xr-x  1 kirby  staff  52 Aug 28 17:24 simDevices@ -> /Users/kirby/Library/Developer/CoreSimulator/Devices

Step Three: Associate SQLite with .sqlite files

We want the Database Browser for SQLite application to be the default application for opening .sqlite files.

Let’s find a .sqlite file in our new simDevices shortcut.

Use the find command to search through all of our Simulator devices looking for the database file associated with our app.

$ cd ~/simDevices
$ find . -name Kody.sqlite
./402FCD3E-7A98-4B5A-9CB1-83B1A3F85B47/data/Containers/Data/Application/AB4D98C4-B86A-4FE3-B557-CFC60C83893E/Documents/Kody.sqlite

Now cd into that directory and open the finder at that location.

NOTE: When you cut and paste the text from your find result, remove the leading ./ characters and the AppName.sqlite filename from the tail.

$ cd 402FCD3E-7A98-4B5A-9CB1-83B1A3F85B47/data/Containers/Data/Application/AB4D98C4-B86A-4FE3-B557-CFC60C83893E/Documents
$ open .

We then right-click on the .sqlite file in finder and set sqlitebrowser to always open files of this type.

SQLite file path

SQLite file path

Step Four: Command line fu

The prep work is complete.

As you create more apps and more databases, how do you easily find and open them in SQLite Browser?

As we saw above, the find command gets us to the file.

$ cd ~/simDevices
$ find . -name Kody.sqlite
./402FCD3E-7A98-4B5A-9CB1-83B1A3F85B47/data/Containers/Data/Application/AB4D98C4-B86A-4FE3-B557-CFC60C83893E/Documents/Kody.sqlite

Almost there!

But we don’t want to just find the file, we want to open it, too.

We’ll use the pipe command | to pass the output from find to the input of the xargs command.

$ find . -name Kody.sqlite | xargs open

And that’s it!

You can now browse and edit (be careful!) your SQLite database.

Browse and edit SQLite database


Check out more resources on Kirby’s blog, Swift Coder.

Next PostPrevious Post

About the Author