以下 SQLite 查询返回 3:
SELECT MAX(depth) FROM mpitree WHERE child = 2
但是此代码的计算结果为无:
def ancestors_depth(self):
self.cursor.execute("SELECT MAX(depth) FROM mpitree WHERE child = 2");
return self.cursor.fetchone()[0]
这是为什么 ?
以下 SQLite 查询返回 3:
SELECT MAX(depth) FROM mpitree WHERE child = 2
但是此代码的计算结果为无:
def ancestors_depth(self):
self.cursor.execute("SELECT MAX(depth) FROM mpitree WHERE child = 2");
return self.cursor.fetchone()[0]
这是为什么 ?
All right, I found the problem. My code was called in a unit test. In this unit test, at first I wrote a test case that populated the database (which was working). Then I added a test for the query. But because tests are executed in alphanumerical order, the test that was querying the database was being executed BEFORE the databases was populated.
Thank you for your help.
您应该存储我认为的查询结果:
SQL_QUERY = "SELECT MAX(depth) FROM mpitree WHERE child = 2"
def ancestors_depth(self):
result = self.cursor.execute(SQL_QUERY)
return result.fetchone()[0]