从python sqlite异常中获取更多信息(Getting more info from python sqlite exceptions)
我正在使用python的内置sqlite3数据库模块 。
在将对象插入我的数据库表时,引发了以下
sqlite
异常:"PRIMARY KEY must be unique"
由于每个对象有不同的插入方法,我无法确定它在哪个表中失败:
import sqlite3 ... class SomeObject1: .... def _insert_some_object1(self, db_object): self._cursor.execute('insert into %s values (?,?,?)' % TABLE_NAME, (db_oject.v1, db_object.v2, db_object_v3,))
except Exception as e:
被main()
捕获,所以它只是我得到的信息。我想知道哪个表插入失败,失败的值等等...
从
sqlite
异常中获取最多信息的正确方法是什么?谢谢
I'm using python's builtin sqlite3 DB module.
While inserting objects to my DB tables, following
sqlite
exception raised:"PRIMARY KEY must be unique"
As there are different insert methods for each object, I can't say for sure in which table does it failed:
import sqlite3 ... class SomeObject1: .... def _insert_some_object1(self, db_object): self._cursor.execute('insert into %s values (?,?,?)' % TABLE_NAME, (db_oject.v1, db_object.v2, db_object_v3,))
Exception got caught in
main()
byexcept Exception as e:
, so it's only info I've got.I would want to know in which table insertion failed, value that failed, etc...
What's the right way to get the most info from
sqlite
exceptions?Thanks
原文:https://stackoverflow.com/questions/34530463
最满意答案
raw_input
的返回值是一个字符串,而不是布尔值。 因此,你is not True
,is not False
测试,尽管它们有明确的含义,意思不是你想要的意思。 您需要比较HEADER
和字符串值。所以你需要例如这样的代码:
if HEADER.lower() == 'true':
我用
tolower()
来进行不区分大小写的比较。 您可能还想剥离空白区域:if HEADER.strip().lower() == 'true':
我相信你可以填写测试来对付你自己。
即使你有一个布尔值,你也不应该使用像
is not True
或is False
代码。 你应该用以下方法测试真相:if somebool:
要么
if not somebool:
因为它更可读。
The return value from
raw_input
is a string and not a boolean. Hence youris not True
andis not False
tests, although they have well-defined meaning, that meaning is not the meaning that you intend. You need to compareHEADER
against string values.So you would need, for example, code like this:
if HEADER.lower() == 'true':
I used
tolower()
to effect case-insensitive comparison. You may also want to strip off white space:if HEADER.strip().lower() == 'true':
I'm sure you can fill in the test against
false
yourself.
Even if you did have a boolean, you should not use code like
is not True
oris False
. You should test for truth with:if somebool:
or
if not somebool:
because it is much more readable.
相关问答
更多-
为什么'True == not False'是Python中的语法错误?(Why is 'True == not False' a syntax error in Python?)[2023-08-18]
它与Python中的运算符优先级有关 (解释者认为你将True与True比较,因为==具有比not优先更高的优先级)。 您需要一些括号来阐明操作顺序: True == (not False) 一般来说, not在没有括号的比较的右侧使用。 但是,我不能想到在某种情况下,您需要使用not比较的右侧。 It has to do with operator precedence in Python (the interpreter thinks you're comparing True to not, sin ... -
来自http://docs.python.org/reference/expressions.html#boolean-operations : 在布尔运算的上下文中,以及当控制流语句使用表达式时,以下值将被解释为false:False,None,所有类型的数字零,空字符串和容器(包括字符串,元组,列表,字典) ,套和frozensets)。 所有其他值被解释为真。 我认为你是误会的关键措辞是“被解释为虚假”或“解释为真实”。 这并不意味着任何这些值与True或False完全相同,甚至等于True或Fals ...
-
为什么“False False False”评估为“True”?(Why does `False is False is False` evaluate to `True`?)[2022-10-19]
像a is b is c链接操作符a is b is c等价于a is b and b is c 。 所以第一个例子是False is False and False is False ,它的计算结果为True and True ,它的计算结果为True 括号会导致一个评估结果与下一个变量进行比较(如您所预期的那样),所以(a is b) is c的结果与(a is b) is c进行比较。 Chaining operators like a is b is c is equivalent to a is ... -
我相信你错误地重载了<=和>=运算符。 代替: def __leq__(self, other): # ... def __geq__(self, other): 改为使用它: def __le__(self, other): # ... def __ge__(self, other): 在进行这些更改并在Python 3.4.1中运行后,我得到: >>> a, b = Omega(), Omega(1) >>> (a+a) <= b True >>> (a+a) <= b True >>> (a+a) ...
-
Python:True或False(Python : While True or False)[2024-02-26]
使用break退出循环: while True: ser = serial.Serial("/dev/ttyUSB0", 4800, timeout =1) checking = ser.readline(); if checking.find(",,,,"): print "not locked yet" else: print "locked and loaded" break True和False行并没有在你的代码中做任 ... -
raw_input的返回值是一个字符串,而不是布尔值。 因此,你is not True , is not False测试,尽管它们有明确的含义,意思不是你想要的意思。 您需要比较HEADER和字符串值。 所以你需要例如这样的代码: if HEADER.lower() == 'true': 我用tolower()来进行不区分大小写的比较。 您可能还想剥离空白区域: if HEADER.strip().lower() == 'true': 我相信你可以填写测试来对付你自己。 即使你有一个布尔值,你也不应该使 ...
-
为什么在python中获得None而不是True / False?(Why do I get None instead of True/False in python? [duplicate])[2022-10-03]
你在最后的else子句中没有返回任何else 。 因此,结果。 def is_sorted(x,i): if i >= len(x): return True elif x[i] <= x[i-1]: return False else: return is_sorted(x,i+1) 演示: >>> def is_sorted(x,i): ... if i >= len(x): ... return True ... -
在Python中,如何从像True = False这样的笑话语句中恢复(In Python how to recover from joke statements like True = False)[2022-09-24]
从这里“恢复”的方法是不要让它发生。 也就是说,你总是可以使用bool()类型来再次访问True和False 。 ( bool()总是返回两个布尔单例之一。) 例: >>> bool>>> bool(1) True >>> bool(1) is bool('true') True >>> True = False >>> True False >>> True is False True >>> False is bool() True >>> True = bool(1) > ... -
我可以发现代码中存在很多问题 - 在你的函数grid_string_maker(word_block, x, y) ,你总是返回pos_word_vert ,你永远不会返回水平的。 这是因为每次您调用函数时,都会从起始位置开始,而不是从中断位置开始。 另外,你不需要太多ifs,你可以直接使用0,1,2作为索引,而不是使用y-1如果y == 1 ,因为你已经知道y是1, y-1是0等。你应该改变这个函数,把垂直和水平的字符串作为一个元组返回,然后把它们接收到单独的字符串中。 在你的函数中 - find_wor ...
-
为什么表达式为false而true为true?(Why is the expression false and true evaluating to true? (python 2.7))[2022-03-10]
>>> print(True and False) False 布尔值的开头有一个大写字母。 我猜你用的是字符串。 您可以使用type(after)检查。 您无需手动划分所有案例以“调试”您的程序。 这不是Python的用途......只需print评估的代码print(after+' and '+late) ,使用类似我的type或使用交互式python控制台来玩。 >>> print(True and False) False Boolean values have a capital letter ...