gpt4 book ai didi

swift - 使用 MySQL for Swift 时如何访问数据库执行错误

转载 作者:行者123 更新时间:2023-11-28 08:13:35 26 4
gpt4 key购买 nike

这是片段(这里的 mysql 是 MySQL for Swift 的一个实例):

do {
try mysql.execute("INSERT INTO ...")
} catch {
print(error) // here will print out the actual MySQL error message
return error.localizedDescription // return "The operation couldn’t be completed. (MySQL.Error error 6.)" The real message from the DB is lost.
}

在 catch 部分的第一行,print 语句能够从 DB 中吐出真正的错误消息,但第二行只返回一个通用语句:操作无法完成。 (MySQL.Error错误6.)

如何从数据库中访问带下划线的错误消息?

最佳答案

您需要专门捕获 MySQLError 以便访问其属性。

} catch let error as MySQLError {
print(error.reason)
}

https://github.com/vapor/mysql/blob/master/Sources/MySQL/Error.swift#L8

关于swift - 使用 MySQL for Swift 时如何访问数据库执行错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43079475/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com