How to find the path of sqlite.sql file and print it? - swift

I know this question already answered before but when I tried to print the path for the sqlite file of my application as below I got the path but could not go to it in finder.
I used below code to get the path
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last! as String
print(paths)
The path I got is as below:
/var/mobile/Containers/Data/Application/5412CDDE-8D40-46B2-A61D-65065796F7CE/Documents
when I try to copy and paste it in GOTO it can not find it
please advise me

In case you are using NSPersistantContainer, you get the URL(s):
persistentContainer.persistentStoreCoordinator.persistentStores.first!.url!

when I try to copy and paste it in GOTO it can not find it
that path is on the Simulator and not on the local Finder.
Duplicate of: Access files in /var/mobile/Containers/Data/Application without jailbreaking iPhone
or
Duplicate of: How to view the data in sqlite file running in iphone application?
How to find the path of sqlite.sql file and print it?
accessing your sqlite database is described here:
Copy Sqlite DataBase in Swift does not work properly

Use this in your swift class to get the location of sqlite database for your application running in iOS simulator.
let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
print(urls[urls.count-1] as URL)
You will need SQLite database browser to look after the database. We can only locate sqlite database file only for application running in simulator but not in the iPhone directly.

Related

read/write database path using sqlite.swift

I'm just starting out trying sqlite.swift and databases with swift. I have prepared a database with tables and preloaded with data. I wish to select data and insert data from within the app.
The problem is that I don't understand where (in my project) to put my database.db file in order for the app to find it.
my connection code:
let path = NSSearchPathForDirectoriesInDomains(
.documentDirectory, .userDomainMask, true
).first!
do {
db = try Connection("\(path)/database.db")
} catch {
db = nil
print ("Unable to open database")
}
In terms of where this file should go, I would suggest the “application support directory”. See File System Programming Guide: Where You Should Put Your App’s Files, which says:
Put user data in Documents/. User data generally includes any files you might want to expose to the user—anything you might want the user to create, import, delete or edit. For a drawing app, user data includes any graphic files the user might create. For a text editor, it includes the text files. Video and audio apps may even include files that the user has downloaded to watch or listen to later.
Put app-created support files in the Library/Application support/ directory. In general, this directory includes files that the app uses to run but that should remain hidden from the user. This directory can also include data files, configuration files, templates and modified versions of resources loaded from the app bundle.
Also see the iOS Storage Best Practices video.
The above is for iOS. If inquiring about macOS, see File System Programming Guide: The Library Directory Stores App-Specific Files.
But regardless of the OS, the technique for referencing the application support directory is largely the same:
do {
let fileURL = try FileManager.default
.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
.appendingPathComponent("database.db")
db = try Connection(fileURL.path)
} catch {
db = nil
print("Unable to open database: \(error)")
}

How to get Core data sqlite file location

I am trying to find the sqlite file location created by core data. I am using the below code:
print("Documents Directory: ", FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last ?? "Not Found!")
And it's showing me the result: ile:///var/mobile/Containers/Data/Application/148690BC-21F4-4239-99B2-5F947023B414/Library/Application Support
But I can't find any sqlite file in the location.
Can any one please help me to solve the issue. I am using Swift 4 and testing my code in iPhone 6sPlus
Unless your app is document-based, the default location for your app's sqlite database is its Application Support directory, not Documents. Change .documentDirectory to .applicationSupportDirectory.

Date still in Core Data app after I delete the .sqlite files

I have (finally) managed to get prototype app using Core Data to persist to an sqlite file. Quitting and restarting I see the same data. So all seems well.
I want to now start from scratch again so I went to delete the sqlite persisted data. I set up it's location as :
open lazy var applicationDocumentsDirectory: NSURL = {
// The directory the application uses to store the Core Data store file.
let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
return urls[urls.count-1] as NSURL
}()
let url = self.applicationDocumentsDirectory.appendingPathComponent("TrainingDiary.sqlite")
which places the .sqlite files in ~/Library/Containers/uk.me.stevenlord.Core-Data-Binding/Data/Documents
There are four files in there: iChats, TrainingDiary.sqlite, TrainingDiary.sqlite-shm and TrainingDiary.sqlite-wal
I close my app. Delete all there files. When I reopen my app the full Core Data model is still there and when I look back in this directory the files are back.
What am I missing ?
EDIT:
I've figured out whats happening though I still don't quite understand why this is how it works. The above code specifies where the Core Data should be saved:
which places the .sqlite files in
~/Library/Containers/uk.me.stevenlord.Core-Data-Binding/Data/Documents/TrainingDiary.sqlite
This it does however I've now found that the app also saves the following
~/Library/Containers/uk.me.stevenlord.Core-Data-Binding/Data/Library/Application Support/Core Data Binding/Core_Data_Binding.sqlite
My XCode9 project is called "Core Data Binding". If I delete this file and start up my app I get an app with no data as expected.
So the persistent store is not going to where I specified. So still confused.
I'm missing something still. But something different

How to hide SQLite db files so they are not seen in iTunes file sharing

I have file sharing enabled on my app and so, when you view the documents folder in iTunes, there are the Core Data sqlite files siting there just waiting to be fiddled with by the user.
I have found a few discussions on this but, surprisingly, no one seems to address the concerns I have.
Some say to 'move' them to the library folder - in a custom sub-directory, and another says just rename the files prefixing each with a period.
Both options sound lovely, but these are essential OS files!
So my first question is, if you do either of these things (with NSFileManager.defaultManager() - I presume), will the app just automatically find them afterwards? ...or is there a specific 'way' in which you do (either of) them ...so that the app finds them afterward?
Any responses, if you could demonstrate using Swift rather than Objective C, that would be appreciated! Thanks, :)
Figured it out:
To anyone wondering the same thing (which I have seen many doing),
In my app delegate > in the 'persistentStoreCoordinator' lazy variable,
I changed the following line of code:
let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("MyAppName.sqlite")
to the following two lines of code:
let library = NSFileManager.defaultManager().URLsForDirectory(.LibraryDirectory, inDomains: .UserDomainMask)[0] as NSURL
let url = libary.URLByAppendingPathComponent("MyAppName.sqlite")
This would be the syntax for Swift 3 / 4:
let library = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask)[0] as NSURL
let url = library.appendingPathComponent("MyAppName.sqlite")

Open sqlite database file inside iphone simulator?

I'm having a hard time trying to understand a problem in my iphone app.
I have a set method in my sqlite database that looks like it is working fine. I do a NSLog when I am setting the value and it is correct. It's a string with the value "2010-10-10 12:13:14".
When I use the get method on the same data, it turns out that the value is not correct. It returns 1.
Is there any way to open the database file that is inside the iPhone simulator so I can see what is the actual data stored in the simulator database?
Is there any way to sniff the sqlite calls between my simulator and the actual file? Something like Charles, or Service Capture?
Many thanks!
Since IOS 8 they put it in a different folder:
Library/Developer/CoreSimulator/Devices/(numbers and letters)/data/Containers/Data/Application/(numbers and letters)/Documents/
Check this answer for more info
If you want to view the database that you have created,
Open finder -> press SHIFT + Command + G -> "~/Library/Application Support/iPhone Simulator" then go.
Open 5.0 (as per your version of simulator)-> Application-> select the pgm folder
-> Documents
Then you can see the database
Update: I made a tool to automate this
Both for Android and iOS, it's available here
Old question, but Apple seem to keep change the logic...
On macOs Sierra you can find the database location by issuing the following commands (the simulator must run during the process!)
ps aux | grep 'CoreSimulator/Devices'
The result should list the path to the simulator, for example:
/Users/user1/Library/Developer/CoreSimulator/Devices/25C1F5DA-E528-4BC2-B684-A92D382553AC/data/var/run/launchd_bootstrap.plist
Navigate to the now found simulator folder:
cd /Users/user1/Library/Developer/CoreSimulator/Devices/25C1F5DA-E528-4BC2-B684-A92D382553AC/Data/Application/
This folder contains all the simulator applications
From here if you know the database name, just find it, like so:
find ./ -type f -name 'my_db.db’
And viola! You have the database path :)
You can refer to the following link below,You can definitely look at the database stored inside the simulator.
How to view the data in sqlite file running in iphone application?
Cheers
For the 6.1 simulator, the location is different.
Start at:
~/Library/Application Support/iPhone Simulator/6.1/Applications/
Open the folder that represents the application on your simulator. The name will look similar this:
0951F670-1DAC-9A39-450A-B8EEFFC0FFE1
If you have multiple folders, then you can click into each folder and look at the app icon until you find the app that you are looking for. Alternatively, uninstall all other apps from the simulator except the one you are interested in.
From inside this folder, go here:
Library/Application Support/<APP_NAME>/<APP_NAME>.sqlite
The total path should look like this:
~/Library/Application Support/iPhone Simulator/6.1/Applications/<APP_ID>/Library/Application Support/<APP_NAME>/<APP_NAME>.sqlite
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(#"path is %#",documentsDirectory);
The above code will print the path of ur database and copy it. Go to finder and choose go to folder from menu (Shift+Win+G) and paste the path here, it will give you the exact path of core data file. Keep this window open.
Next step Go to Mozilla firefox click on menubar choose Tools -> SQLite Manager. It will open new window. In new window click on open file and drag that path of coredata file in current window and click on open.
Note - You must have Installed SQLite Manager in your Mozilla firefox.
For 2019 and 2020 answer: ⚠️
This is easy. Use a free-opensourced application called Simsim (https://github.com/dsmelov/simsim).
Read the README and there's a direct download link there.
Choose Finder, and from there, you can find the .sqlite db file in the Documents or use the search function of the Finder targeting your application folder.
Xcode 12.5+
Find the path to .sqlite
let fileManager = FileManager.default
let databasePath = try fileManager.url(
for: .applicationSupportDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: true
)
.appendingPathComponent("yourDatabaseName.sqlite")
.path
go to the file
Open finder -> press SHIFT + Command + G -> past the "file path" then go.
view sqlite file and query it
https://inloop.github.io/sqlite-viewer/