3

I'm trying to open Messages db (~/Library/Messages/chat.db) using SQLite3 lib:

int rc = sqlite3_open_v2("/Users/username/Library/Messages/chat.db", &m_db, SQLITE_OPEN_READONLY, NULL);
if (rc != SQLITE_OK) {
      fprintf(stderr, "Cannot open database [%d]: %s\n",
              sqlite3_extended_errcode(m_db),
              sqlite3_errmsg(m_db));
              sqlite3_close(m_db);
       } else {
              qDebug() << "Database: connection ok";
       }

It returns

Cannot open database [14]: unable to open database file

where 14 is SQLITE_CANTOPEN according to https://www.sqlite.org/rescode.html#cantopen

After making a copy of chat.db file it opens correctly with code above.

In the same time, using built-in cli, I'm able to get access to db at original path from terminal:

> sqlite3 ~/Library/Messages/chat.db

SQLite version 3.22.0 2018-01-22 18:45:57
Enter ".help" for usage hints.
sqlite> .tables
_SqliteDatabaseProperties  kvtable
attachment                 message
chat                       message_attachment_join
chat_handle_join           message_processing_task
chat_message_join          sync_deleted_attachments
deleted_messages           sync_deleted_chats
handle                     sync_deleted_messages

How to open db programmatically?

poldueta
  • 41
  • 1
  • 2
  • Seems strange. Is it possible that you were locking access to the file and then through the copy removed the lock? – Gardener Oct 29 '18 at 20:04
  • @JohnMurray Actually it's quite possible that file can be locked by system since that's an iMessage db store and used by Messages.app all the time. But using cli sqlite3 it's possible to access db at any time. That's strange. Maybe it doing shadow copy while before open db? I suppose that clue is on a WAL-related stuff. – poldueta Oct 29 '18 at 21:19

0 Answers0