24/7 twenty-four seven

iOS/OS X application programing topics.

CoreData で SQLite のデバッグログを出力するには

Analyzing Fetch Behavior with SQLite
With Mac OS X version 10.4.3 and later, you can use the user default com.apple.CoreData.SQLDebug to log to stderr the actual SQL sent to SQLite. (Note that user default names are case sensitive.) For example, you can pass the following as an argument to the application:

-com.apple.CoreData.SQLDebug 1
Mac Developer Library

アプリケーション起動時の引数に下記を指定します。

-com.apple.CoreData.SQLDebug 1


プロジェクト>アクティブな実行可能ファイル"アプリケーション名"を編集
(または、「実行可能ファイル」を選択して「情報を見る」)


起動時に渡される引数に設定します。


実行結果は下記のようになります。
発行されている SQL (SELECT, INSERT, COMMIT, etc) やフェッチにかかった時間などが出力されます。

2010-03-12 21:51:40.105 TopSongs[12129:207] CoreData: annotation: Connecting to sqlite database file at "/Users/katsumi/Library/Application Support/iPhone Simulator/User/Applications/FCA26FFE-7023-4832-8D2B-E511DED2B5DD/Documents/TopSongs.sqlite"
2010-03-12 21:51:40.107 TopSongs[12129:207] CoreData: annotation: creating schema.
2010-03-12 21:51:40.112 TopSongs[12129:207] CoreData: sql: pragma page_size=2048
2010-03-12 21:51:40.113 TopSongs[12129:207] CoreData: sql: pragma auto_vacuum=2
2010-03-12 21:51:40.114 TopSongs[12129:207] CoreData: sql: BEGIN EXCLUSIVE
2010-03-12 21:51:40.115 TopSongs[12129:207] CoreData: sql: CREATE TABLE ZCATEGORY ( Z_PK INTEGER PRIMARY KEY, Z_ENT INTEGER, Z_OPT INTEGER, ZNAME VARCHAR ) 
2010-03-12 21:51:40.115 TopSongs[12129:207] CoreData: sql: CREATE INDEX ZCATEGORY_ZNAME_INDEX ON ZCATEGORY (ZNAME)
2010-03-12 21:51:40.116 TopSongs[12129:207] CoreData: sql: CREATE TABLE ZSONG ( Z_PK INTEGER PRIMARY KEY, Z_ENT INTEGER, Z_OPT INTEGER, ZRANK INTEGER, ZCATEGORY INTEGER, ZRELEASEDATE TIMESTAMP, ZARTIST VARCHAR, ZALBUM VARCHAR, ZTITLE VARCHAR ) 
2010-03-12 21:51:40.117 TopSongs[12129:207] CoreData: sql: CREATE INDEX ZSONG_ZCATEGORY_INDEX ON ZSONG (ZCATEGORY)
2010-03-12 21:51:40.118 TopSongs[12129:207] CoreData: annotation: Creating primary key table.
2010-03-12 21:51:40.119 TopSongs[12129:207] CoreData: sql: CREATE TABLE Z_PRIMARYKEY (Z_ENT INTEGER PRIMARY KEY, Z_NAME VARCHAR, Z_SUPER INTEGER, Z_MAX INTEGER)
2010-03-12 21:51:40.123 TopSongs[12129:207] CoreData: sql: INSERT INTO Z_PRIMARYKEY(Z_ENT, Z_NAME, Z_SUPER, Z_MAX) VALUES(1, 'Category', 0, 0)
2010-03-12 21:51:40.125 TopSongs[12129:207] CoreData: sql: INSERT INTO Z_PRIMARYKEY(Z_ENT, Z_NAME, Z_SUPER, Z_MAX) VALUES(2, 'Song', 0, 0)
2010-03-12 21:51:40.125 TopSongs[12129:207] CoreData: sql: CREATE TABLE Z_METADATA (Z_VERSION INTEGER PRIMARY KEY, Z_UUID VARCHAR(255), Z_PLIST BLOB)
2010-03-12 21:51:40.127 TopSongs[12129:207] CoreData: sql: SELECT TBL_NAME FROM SQLITE_MASTER WHERE TBL_NAME = 'Z_METADATA'
2010-03-12 21:51:40.128 TopSongs[12129:207] CoreData: sql: DELETE FROM Z_METADATA WHERE Z_VERSION = ?
2010-03-12 21:51:40.131 TopSongs[12129:207] CoreData: sql: INSERT INTO Z_METADATA (Z_VERSION, Z_UUID, Z_PLIST) VALUES (?, ?, ?)
2010-03-12 21:51:40.132 TopSongs[12129:207] CoreData: sql: COMMIT
2010-03-12 21:51:40.140 TopSongs[12129:207] CoreData: sql: pragma cache_size=1000
2010-03-12 21:51:40.142 TopSongs[12129:207] CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZARTIST, t0.ZRELEASEDATE, t0.ZALBUM, t0.ZTITLE, t0.ZRANK, t0.ZCATEGORY FROM ZSONG t0 ORDER BY t0.ZRANK
2010-03-12 21:51:40.143 TopSongs[12129:207] CoreData: annotation: sql connection fetch time: 0.0006s
2010-03-12 21:51:40.143 TopSongs[12129:207] CoreData: annotation: total fetch execution time: 0.0010s for 0 rows.
2010-03-12 21:51:40.825 TopSongs[12129:4503] CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZNAME FROM ZCATEGORY t0 WHERE  NSCoreDataLike( t0.ZNAME, ?, 0)  
2010-03-12 21:51:40.826 TopSongs[12129:4503] CoreData: annotation: sql connection fetch time: 0.0014s
2010-03-12 21:51:40.826 TopSongs[12129:4503] CoreData: annotation: total fetch execution time: 0.0017s for 0 rows.
2010-03-12 21:51:40.828 TopSongs[12129:4503] CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZNAME FROM ZCATEGORY t0 WHERE  NSCoreDataLike( t0.ZNAME, ?, 0)