V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Tornado Documentation
http://www.v2ex.com/tornado/
Tornado on GitHub
https://github.com/facebook/tornado/
Tornado Gists
http://tornadogists.org/
Plumes
V2EX  ›  Tornado

请问在使用 Torndb 时如何捕获 mysql 的具体错误?

  •  
  •   Plumes · 2014-05-25 19:16:49 +08:00 · 4743 次点击
    这是一个创建于 3614 天前的主题,其中的信息可能已经有所发展或是发生改变。
    举个例子:
    想实现用户的注册功能,用户名唯一,直接 INSERT 得到的用户名和密码,当捕获到 "Duplicate entry" 错误时返回用户名重复消息。
    但是发现 torndb 返回的消息为 IntergrityError: (1062, "Duplicate entry ...") 这样的,请问怎样能捕获到 "Duplicate entry" 这个错误,而不只是捕获到 IntergrityError?
    或者有更优雅的方式实现?
    1 条回复    2014-05-25 19:44:27 +08:00
    dbow
        1
    dbow  
       2014-05-25 19:44:27 +08:00
    不懂torndb,只是Duplicate entry看起来是IntergrityError的一种,如果想只针对Duplicate entry可以用
    try:
    ....
    except IntergrityError as e:
    if 1062 not in e.args:
    raise e
    //your code
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3147 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 14:36 · PVG 22:36 · LAX 07:36 · JFK 10:36
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.