可以捕获连接错误点击室DB API

发布于 2025-02-05 08:29:56 字数 375 浏览 2 评论 0原文

我正在尝试使用Python中的Clickhouse-Driver DB API连接到KX时捕获连接错误。但是由于某种原因,尝试:块通过没有错误的情况,而我没有例外

def __enter__(self):
try:
        self.connector = db.connect(dsn=self.connection_string)
    except Error as e:  # pass
        self.error = str(e)[:250]
        self.connector = None
    return self 

,并且仅在执行执行 sql时才会发生错误。在连接时有什么方法可以遇到连接错误?

I'm trying to catch a connection error when connecting to kx using the clickhouse-driver db api in python. But for some reason, the try: block passes without errors, and I don't get exception

def __enter__(self):
try:
        self.connector = db.connect(dsn=self.connection_string)
    except Error as e:  # pass
        self.error = str(e)[:250]
        self.connector = None
    return self 

And the error only happens when execute sql is executed. Is there any way to get a connection error at the moment of connection?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雾里花 2025-02-12 08:29:56

我添加了execute('select 1')。因为只有在执行后,我们才创建类的实例,因此在此之后,我才能获得连接异常。

def __enter__(self):
    try:
        self.connector = dbp.connect(dsn=self.connection_string)
        self.cur = self.connector.cursor()
        self.cur.execute('select 1')
    except dbp.Error as e:
        self.error = '\nConnection error occurred: ' + '\n' + str(e)[:250]
        self.connector = None
    return self

I added execute('select 1'). Because only after execute we are creating instance of class, so after that I can get connection exception.

def __enter__(self):
    try:
        self.connector = dbp.connect(dsn=self.connection_string)
        self.cur = self.connector.cursor()
        self.cur.execute('select 1')
    except dbp.Error as e:
        self.error = '\nConnection error occurred: ' + '\n' + str(e)[:250]
        self.connector = None
    return self
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文