小编Gam*_*ion的帖子

识别sqlalchemy.exc.OperationalError

我试图捕获mysql/sqlalchemy OperationalErrors并替换句柄访问被拒绝(1045)与连接被拒绝(2003)

sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (1045, "Access denied for user … (Background on this error at: http://sqlalche.me/e/e3q8)
sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on 'localhost' ([Errno 111] Connection refused)") (Background on this error at: http://sqlalche.me/e/e3q8)
Run Code Online (Sandbox Code Playgroud)

我似乎无法找到任何关于如何以编程方式区分这些文档的文档.我潜入了资源,并认为我可以检查err.orig.original_exception.errno的值,但事实并非如此.

编辑:似乎没有为访问被拒绝定义err.orig,这可能是一个错误.

try:
  engine.scalar(select([1]))
except sqlalchemy.exc.OperationalError as err:
  if err_______:
    print("Access Denied")
  elifif err_______:
    print("Connection Refused")
  else:
    raise
Run Code Online (Sandbox Code Playgroud)

这个问题确实让我感到烦恼,甚至连赏金都没有消息.我开始相信它必须是sqlalchemy中的一个错误,但sqlalchemy文档在这方面并不是很具描述性,而且我对sqlalchemy和python很新,所以我真的很难说.我也找不到对irc的支持,我从哪里开始?

python sqlalchemy python-db-api

7
推荐指数
1
解决办法
477
查看次数

正确施放签名到未签名

我正在使用一个C库,它使用无符号整数作为某些数据的索引.但有时,函数会将这些索引返回为signed,以便-1在函数无法返回索引时返回.*

implicit conversion changes signedness如果无法进行转换,如何防止警告,而是抛出运行时错误?您是否建议将库函数包装为使用异常进行错误处理并仅返回正确的值?有没有标准的方法来做到这一点:

#include <stdlib.h>
#include <errno.h>
#include <limits.h>

// pointless c function to demonstrate the question
// parse the string to an unsigned integer, return -1 on failure
int atoui(char const* str) {
    char* pend;
    long int li=strtol(str, &pend, 10);
    if ( errno!=0 || *pend!='\0' || li<0 || li>INT_MAX ) {
        return -1;
    } else {
        return li;
    }
}

// --8<---

#include <stdexcept>

// How to do this properly?
unsigned int unsign(int i) …
Run Code Online (Sandbox Code Playgroud)

c++

3
推荐指数
2
解决办法
106
查看次数

标签 统计

c++ ×1

python ×1

python-db-api ×1

sqlalchemy ×1