【发布时间】:2015-09-10 12:57:02
【问题描述】:
我正在循环中从数据库中获取记录行。在循环中还有一些选择查询,其中重新记录不退出..
我写了这个视图
CREATE view get_application_views AS
SELECT application_id, coalesce(SUM(views),0) as views FROM employer_applicationstats GrOUP BY application_id;
然后在这个循环中..
SELECT views FROM get_application_views WHERE application_id = '5605617';
对于application_id 5605617,没有记录存在,所以我的python脚本引发错误并停止循环。s
我该如何处理这种情况?
更新:
def get_application_view(application_id):
try:
cursor = connection.cursor()
sql_query = "SELECT views FROM get_application_views WHERE application_id = '"+str(application_id)+"';"
cursor.execute(sql_query)
except Exception as e:
print e
status = 0
return status
else:
ver = cursor.fetchone()
status = ver[0]
return status
对于 application_id = 5605617 它说,
status = ver[0]
TypeError: 'NoneType' object has no attribute '__getitem__'
【问题讨论】:
-
我们应该怎么知道?您没有显示任何脚本。
-
立即查看更新问题
标签: python postgresql function views