failed to open database chat.db on Xcode's project(swift)

as I'm saying, I'm trying to open database(~/Library/Messages/chat.db) on a swift project in Xcode, but I got some warning like

(2024-04-10 17:12:11.867139+0800 listenFunction_v0[1564:74241] Metal API Validation Enabled

Error: authorization denied (code: 23))

I know it maybe existing some permission issues, and I added Xcode to full disk access, and I'm sure that permissions of chat.db are "rwx-rwx-rwx". I did everything I can, however it still warning same as I said, it doesn't work😭😭😭😭 I used to write the same code(same function, just taking sql query on chat.db) in AppleScript it worked as I hope, or execute the command line directly with the terminal. btw, I'm coding on macOS 12.2.1 and Xcode 13.4. I wish someone can help me solve this problem if possible😭😭😭😭😭


here is some of my code:


import SQLite

let messages = Table("messages")

let id = Expression<Int>("id")

let sender = Expression<String?>("sender")

let text = Expression<String>("text")

func performSQLQuery() {

    do {

        let dbPath = "/Users/Mac/Library/Messages/chat.db"

        let db = try Connection(dbPath)

        for message in try db.prepare(messages) {

            print("ID: \(message[id]), Sender: \(message[sender]), Text: \(message[text])")

        }

    } catch {

        print("Error: \(error)")

    }

}

performSQLQuery()


MacBook Pro 13″, macOS 12.2

Posted on Apr 10, 2024 2:45 AM

Reply
1 reply

Apr 24, 2024 9:30 AM in response to zeongit

It seems like you've taken the necessary steps to grant Xcode full disk access and ensure that the permissions for the `chat.db` file are correctly set. Since you're still encountering authorization denied errors, there might be a few additional things to consider.


Firstly, ensure that the path to the `chat.db` file is correct. In your code, you've specified the path as `"/Users/Mac/Library/Messages/chat.db"`. However, the actual path might be different, especially since you're using the tilde `~` to represent the user's home directory. You can try printing out the `dbPath` variable to double-check the path being used.


Another thing to consider is sandboxing. If your Xcode project is sandboxed, it might have limited access to certain directories, even with full disk access granted. In this case, you might need to explicitly request permission to access the `chat.db` file using `NSOpenPanel` or similar APIs.


Additionally, make sure that the SQLite library you're using in your Swift project is properly configured to work with macOS and Xcode. You might need to check the documentation or community forums for the SQLite library to see if there are any specific instructions or considerations for using it in a macOS Swift project.


If none of these suggestions resolve the issue, you might consider reaching out to the SQLite library's support channels.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

failed to open database chat.db on Xcode's project(swift)

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.