【发布时间】:2013-10-23 07:50:27
【问题描述】:
到目前为止,这是我的代码。如果 MySQL 没有返回任何结果,我正在尝试打印 No results found,但我无法弄清楚。也许我使用了不正确的论点。谁能给我一个例子?非常感激!
def movie_function(film):
connection = mysql connection info
cursor = connection.cursor()
sql = "SELECT * FROM film_database WHERE film_name = '"+film+"' ORDER BY actor"
cursor.execute(sql)
rows = cursor.fetchall()
for row in rows:
print row[1]
【问题讨论】:
-
你试过什么?我看不到您要在哪里打印“未找到结果”。提示它需要在
if声明中。 -
只检查行的长度。如果 len(rows) == 0,打印 'No results found'
-
你可以自己检查行
if not rows:
标签: python mysql python-2.7