来自psycopg2
上的PostgreSQL docs:
当已经在事务块内部时发出BEGIN将激发a 警告信息。交易状态不受影响。
如何让let allMedia = PHAsset.fetchAssetsWithOptions(fetchOptions)
let allPhotos = PHAsset.fetchAssetsWithMediaType(.Image, options: fetchOptions)
let allVideo = PHAsset.fetchAssetsWithMediaType(.Video, options: fetchOptions)
print("Found \(allMedia.count) media")
print("Found \(allPhotos.count) images")
print("Found \(allVideo.count) videos")
针对任何此类警告提出异常?
答案 0 :(得分:2)
我离psycopg2
或Postgres
专家很远,而且我确信有更好的解决方案来提高警告级别,但这里有一些对我有用的东西 - {{3调查a custom cursor,如果有什么东西 - 它会抛出异常。实现本身主要用于教育目的 - 我确信需要调整它以适应您的用例:
import psycopg2
# this "cursor" class needs to be used as a base for custom cursor classes
from psycopg2.extensions import cursor
class ErrorThrowingCursor(cursor):
def __init__(self, conn, *args, **kwargs):
self.conn = conn
super(ErrorThrowingCursor, self).__init__(*args, **kwargs)
def execute(self, query, vars=None):
result = super(ErrorThrowingCursor, self).execute(query, vars)
for notice in self.conn.notices:
level, message = notice.split(": ")
if level == "WARNING":
raise psycopg2.Warning(message.strip())
return result
使用示例:
conn = psycopg2.connect(user="user", password="secret")
cursor = conn.cursor(conn, cursor_factory=ErrorThrowingCursor)
如果在执行查询后发出警告,则会抛出异常(connection notices类型)。样品:
psycopg2.Warning: there is already a transaction in progress