IPhone core data simulator - iphone

I've created an app that uses core data. Is there a way to view the sqlite db on the simulator? A tool perhaps that allows to query the sqlite db on the simulator? Like a data browser?
Is there a way to browse the iphone simulator to the sqlite db location?

With Xcode 6, the simulator base directory can be found here:
~/Library/Developer/CoreSimulator/Devices/<device-id>/data/Containers/Data/Application
Then, the database itself can be found in the documents directory:
<app-id>/Documents/<name>.sqlite
The database can be accessed using sqlite3 on the command line.

Xcode 5
I just downloaded a trial of Base and was able to point it at the .sql database from the simulator via:
/Macintosh HD/Users/"username"/Library/Application Support/iPhone Simulator/"current iOS development version"/Applications/
The folder names for the applications are unreadable, but if you click on the folder you should be able to see the contents with familiar names.
Xcode 6+
This was changed in Xcode 6, and now is located at ~/Library/Developer/CoreSimulator/Devices/<device-id>/data/Containers/Data/Appl‌​action where the database itself can be found within the documents directory <app-id>/Documents/<name>.sqlite

Try Lita, free and quite good.

If you have the free sqlite3 installed, you can do so at the command line by doing:
/Users/<user>/Library/Application\ Support/iPhone\ Simulator/<sim_version>/Applications/<apphash>/Documents/<yourfile>
Easiest way to see which hash you should peek in:
ls -lt /Users/<user>/Library/Application\ Support/iPhone\ Simulator/<sim_version>/Applications/
You can run .tables at the prompt to see what tables are there, and .schema <tablename> to check out its columns.

i´m quite happy with SQLite Manger for Firefox. Free AddOn.

This is a quite an old post, however I was looking for something else and I landed here, my 2 cents:
In reply to WINSergey:
(1) The easiest way to recover your device ID and the entire path is to print:
NSLog(#"app dir: %#",[[[NSFileManager defaultManager]
URLsForDirectory:NSDocumentDirectory
inDomains:NSUserDomainMask] lastObject]);
Then you'll see the output on the Debugging area or opening the System Log from the simulator.
(2) Also, to know the Device ID from the simulator: XCode > WIndow > Devices > > it shows the Identifier on the right hand side.
I'm also using SQLiteStudio, it is Open Source and works well.

Related

Iphone SQLite problems

I'm developing an iphone application that read data through the sqlite.
I created a database through Terminal and added to the Xcode project.
I went back to the terminal and includes a new table in the file. sql. The problem is because my new queries do not see the new tables. This is kind a cache? How do i solve this problem?
Where is the SQLite file? Is it in your application bundle? Make sure you modify the version that's been copied to your iPhone Simulator folder (~/Library/Application Support/iPhone Simulator/) if you’re trying to modify the version running in the simulator.
What code do you use to create the database? Are you checking to see if it exists before copying it to your documents folder? If so, it exists and won't be copied.
You might try uninstalling your app from the simulator (or device) and reinstalling the app presumably with the new SQLite database. There's a few ways to get the old database in this situation.

How do i test my app data persistence during development?

My question is related to the following three questions.
iPhone What happens to previous data when app is upgraded to new version
How the application upgrade works in iPhone
Preventing erasure of user data while upgrading iOS application via iTunes
Here are my 2 questions.
Am I supposed to create a "Documents" directory manually on filesystem or create just a "group" in xcode, so that when app is upgraded, data stay persistant?
How I can test the app persistance during the app development on simulator or on IPhone?
Thanks.
Neither. The Documents directory is created for you when your app is installed. You can get the path to it using:
NSString *docuDir = nil;
NSArray *list = NSSearchPathForDirectoriesInDomains(
NSDocumentsDirectory, NSUserDomainMask, YES/*expand tilde*/);
if ([list count]) docuDir = [list objectAtIndex:0U];
As #Ben said, the directory is only deleted when you explicitly uninstall the app (delete it from within Springboard). When you build and run, Xcode effectively does an in-place upgrade of your app. If everything is intact then (and there's no reason it wouldn't be), you should be fine.
When the app is upgraded your Documents folder is left intact. You can copy files into the Documents directory the first time your app is run such as a sqlite file for example, obviously only if it doesn't already exist. If you use CoreData then it will automatically store your data there. You will need to be able to run migration scripts if your data schema changes on upgrade, so keep that in mind. CoreData has migration features to take care of this for you and of course you can do it manually too.
When you run an app in the simulator it also leaves the Documents directory intact, so you have to delete the app from the simulator to simulate a fresh install. An upgrade is simulated simply by running your code since the Documents folder was created on the 'fresh install' first run.
Another way to approach this would be look at the Documents folder. If you are running your app in the simulator, then your app's Documents folder will have a path something like this:
/Users/itsaboutcode/Library/Application Support/iPhone Simulator/4.1/Applications/SOME_LONG_HEX_KEY_THAT_MAPS_TO_YOUR_APP/
If you are testing on an iDevice, then use Xcode's Organizer window's Summary tab. At the bottom there's a section called Applications. You app will be near the top of that list and it will have a toggle triangle - which you will click on. This will reveal "Application Data" which can be downloaded by clicking on downward pointing arrow to the right.
The iPhone Simulator has the advantage that the Documents folder and its contents can be modified at will. The Application Data on iDevice is read-only (except, of course, for your app at runtime).

Where is the sqlite3 database stored on the iphone emulator?

Now that I have a read-only application working, I am working on the insert statement. The insert returned OK in my code, but the read-back (in the code) came up empty, so I want to use the command line or browser to read. Can I copy the DB off the emulator and into my laptop for access with other utilities?
You can access the documents directory of your app at ~/Library/Application Support/iPhone Simulator/<SDK VERSION>/Applications/<SOME RANDOM HASH>/Documents
Note that the SDK version will be different depending on what you are building under, and the hash is tough to figure out which one is yours. Just inspect the folders and look for a YOURAPP.app binary to figure it out.
What database?
Assuming you have created it in your app's documents folder, app sandboxes for the simulator are in ~/Library/Application Support/iPhone Simulator/<version>/<uuid>, and contain the application and it's Documents, Library and tmp directories.

location of database created by the application

I have opened a database using sqlite3 in my application. When i run the application in the simulator, this database might be created and i'm playing with it. But now i need to know the location (to be accessed from ma mac) where this database is created. Surely it is not in the application folder as i'm not seeing it. I doubt it to be somewhere in the location where the simulator is installed. Anyone knowing the location. pls help me finding it.
If you're looking for the actual file on your Mac, try looking in this folder:
/Users/YOURUSERNAME/Library/Application Support/iPhone Simulator/User/Applications/
There should be a bunch of folders with long names. If you've recently built your app for the Simulator, order the folder by Date and the newest will be at the top. The folder contains a Documents folder, but you can also right-click on the app to "Show Package Contents", which might contain your database.sqlite file.

Importing AddressBook data into the iPhone Simulator

Is there an easy way to import AddressBook data into the iPhone Simulator?
Right now my only assumption is to manually modify the SQLite files found in the /Library/Application Support/iPhone Simulator/User/Library/AddressBook.
I was looking for the exact solution, and this worked perfectly for me on a non-jailbroken phone. It works by extracting your iPhone address book from an unencrypted backup.
Quit the iOS Simulator
Download iPhone/iPod Touch Backup Extractor
Run it, and select the last option "iOS Files"
Create a folder on your desktop and extract all files to that folder
When complete, go to your extract folder and navigate to Library/AddressBook
Copy both files (AddressBook.sqlitedb and AddressBookImages.sqlitedb) to:
~/Library/Application Support/iPhone Simulator/{SDK}/Library/AddressBook
Run the Contacts application in iOS Simulator
DummyContacts is an application that allows you to add Contacts to AddressBook and can be a handy tool for all iOS devices as well as simulators. DummyContacts is a life saver and pretty easy to setup. One click adds 100 records and one click also removes all 100. It also supports iOS6. So check it out at : https://github.com/Janak-Nirmal/DummyContacts .
Here's a simple app I've made which you can install in the simulator
https://github.com/cristianbica/CBSimulatorSeed
Just sign in to your iCloud account on the simulator and turn "Contacts on" under iCloud. All your contacts, photos etc will appear on the simulator. Obviously, this assumes you are backing up your contacts to iCloud on your iPhone. Hope it helps!
As Marc pointed in his answer https://stackoverflow.com/a/27582281/4598931 for a similar question it can be done just exporting contacts to vCard and drag and drop the generated file to iphone simulator.
or if your iPhone isn't jailbroken, you can extract the sqlite db from a backup using the shell script here: http://0xced.blogspot.com/2009/01/using-your-own-address-book-in-iphone.html
(note you'll need to change the simulator path it copies it to, as in the current SDK it's a bit different due to the addition of the OS version folder - easiest thing would be to just change it to your home directory and copy manually from there)
Right this is the only way i know with a "legit" iPhone
But if you have a jailbroken iPhone, you can download the iPhone AddressBook database with ssh, and replace the simulator one by the iphone one.