【问题标题】:Django / Python: catch MySQL IntegrityErrorDjango / Python:捕获 MySQL IntegrityError
【发布时间】:2013-02-08 10:00:27
【问题描述】:

在 Django/Python 中,如何捕获特定的 mySQL 错误:'IntegrityError':

try:
   cursor.execute(sql)
except IntegrityError:
    do_something

不确定我应该导入什么以及从哪里导入。

【问题讨论】:

    标签: python mysql django


    【解决方案1】:

    你试过from django.db import IntegrityError吗?

    【讨论】:

      【解决方案2】:

      Here 是一个类似的响应。总结一下

      from django.db import IntegrityError
      from django.shortcuts import render_to_response
      
      try:
      # code that produces error
      except IntegrityError as e:
      return render_to_response("template.html", {"message": e.message})
      

      【讨论】:

        猜你喜欢
        • 2015-01-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-16
        • 1970-01-01
        • 2014-08-22
        • 1970-01-01
        相关资源
        最近更新 更多