Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 异常和sqlite3_Python_Sql_Sqlite_Flask - Fatal编程技术网

Python 异常和sqlite3

Python 异常和sqlite3,python,sql,sqlite,flask,Python,Sql,Sqlite,Flask,我试图弄清楚上面的代码是否有效。也就是说,在我的“try”子句之后可以有多个异常吗 当我通过键入“python filename.py”来运行此命令时,我看到终端中打印了“哎呀!这是一个操作错误。请重试…” 这是有道理的,因为我把第一个表的名称拼写错了(locations而不是locations),所以这是一个操作错误 但我对如何强制出现名称错误或值错误感到困惑 另外,当我注释掉“except sqllite3.OperationalError”子句时,我在终端中得到了以下错误: import

我试图弄清楚上面的代码是否有效。也就是说,在我的“try”子句之后可以有多个异常吗

当我通过键入“python filename.py”来运行此命令时,我看到终端中打印了“哎呀!这是一个操作错误。请重试…”

这是有道理的,因为我把第一个表的名称拼写错了(locations而不是locations),所以这是一个操作错误

但我对如何强制出现名称错误或值错误感到困惑

另外,当我注释掉“except sqllite3.OperationalError”子句时,我在终端中得到了以下错误:

import sqlite3
conn = sqlite3.connect("database.db")
cursor = conn.cursor()
try:
    cursor.execute("INSERT INTO loctions VALUES('Hogwarts', 'Scotland'")
    cursor.execute("INSERT INTO characters VALUES('Albus', 'Dumbledore')")
    conn.commit()

except sqlite3.OperationalError:
    print "Oops! This was an operational error. Try again..."

except sqlite3.NameError:
    print "Name Error"

except sqlite3.ValueError:
    print "value error"

except sqlite3.IOError:
    print "IO error"

conn.close()
回溯(最近一次呼叫最后一次):
文件“sqle.py”,第14行,在
除sqlite3.NameError外:
AttributeError:“模块”对象没有属性“NameError”
这是否意味着不存在sqlite3.NameError之类的东西?然而sqlite3.0错误是一件事

如何发现存在哪些类型的错误?

,是。不要用
sqlite3来限定它们。

Traceback (most recent call last):
  File "sqle.py", line 14, in <module>
    except sqlite3.NameError:
AttributeError: 'module' object has no attribute 'NameError'

要查看模块中定义了哪些异常(使用异常是
Exception
类的子类的属性):

或参考以下内容。不要用
sqlite3来限定它们。

Traceback (most recent call last):
  File "sqle.py", line 14, in <module>
    except sqlite3.NameError:
AttributeError: 'module' object has no attribute 'NameError'

要查看模块中定义了哪些异常(使用异常是
Exception
类的子类的属性):


或咨询

这对任何其他软件包(如peewee)也很有帮助。谢谢。这对任何其他套餐(如peewee)也很有帮助。非常感谢。
>>> import inspect
>>> [name for name, value in vars(sqlite3).items()
     if inspect.isclass(value) and issubclass(value, Exception)]
['Warning', 'InternalError', 'ProgrammingError', ..., 'OperationalError']