How to open chat.db for reading

Viewed 3729

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?

0 Answers
Related