25

How can I print the values being sent to the sqlite database while using Core Data SQL Debug?

Using the "-com.apple.CoreData.SQLDebug 1" in my "Arguments Passed on Launch" debug options prints the SQL structure just fine

(you can check see how to use this here: XCode4 and Core Data: How to enable SQL Debugging)

But the problem is that the NSLog printed is something like UPDATE ZTABLE SET ZCOLUMN = ? WHERE ZID = ? and it does not help at all if you are trying to see what is the full SQL statement and/or data being sent to the database.

2 Answers 2

41

Open "Product -> Scheme -> Edit Scheme..." in Xcode and add to "Arguments Passed on Launch":

-com.apple.CoreData.SQLDebug 3
-com.apple.CoreData.Logging.stderr 1 

(The second launch argument is needed for Core Data debugging on iOS 10/macOS 10.12 or later, see com.apple.CoreData.SQLDebug not working for more information.)

The you'll see all the values that the SQL statements are bound to. Example output:

test56[1588:c07] CoreData: sql: BEGIN EXCLUSIVE
test56[1588:c07] CoreData: sql: INSERT INTO ZEVENT(Z_PK, Z_ENT, Z_OPT, ZTIMESTAMP) VALUES(?, ?, ?, ?)
test56[1588:c07] CoreData: details: SQLite bind[0] = (int64)13
test56[1588:c07] CoreData: details: SQLite bind[1] = (int64)1
test56[1588:c07] CoreData: details: SQLite bind[2] = (int64)1
test56[1588:c07] CoreData: details: SQLite bind[3] = "368650709.435904"
test56[1588:c07] CoreData: sql: COMMIT
4
  • It is not showing NSFetchRequest please let me know is there any way to show that also Jul 13, 2017 at 8:47
  • @VarunNaharia: You can simply print the request, something like NSLog(@"request: %@", request)
    – Martin R
    Jul 13, 2017 at 9:27
  • I am using swift and I checked po fetchRequest on console and I got this <NSFetchRequest: 0x1704c23e0> (entity: TravelDetail; predicate: (id == 6412); sortDescriptors: ((null)); type: NSManagedObjectResultType; ) Jul 13, 2017 at 9:33
  • @VarunNaharia: In Swift you can do print("request:", request).
    – Martin R
    Jul 13, 2017 at 9:37
4

If you are using Xcode 8, you will need to also need to add additional argument

-com.apple.CoreData.Logging.stderr 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.