我做了一个程序事务。如果错误,它应该报告位置错误。现在我想选择值TRUE和DELETE值TRUE
#!/usr/bin/python
import mysql.connector
conn = mysql.connector.connect(host="lll",user="ppp",passwd="ppp",db="ppp")
cursor = conn.cursor()
cursor.execute("SELECT(case when user1 = '1' THEN 'true' ELSE 'false' END) AS IsEmtpy from dt")
print(cursor.fetchall())
try:
if true:
cursor.execute("DELETE FROM dt WHERE user1='1'")
print "DELETE SUCESS"
else:
print "DELETE ERROR"
conn.commit()
except Exception as e:
conn.rollback()
conn.close()
结果:
File "./splinsert.py", line 13
if true:
^
IndentationError: expected an indented block
答案 0 :(得分:1)
此错误与不一致的缩进有关。 Python依赖缩进来确定何时“代码块”启动和停止。 Take a look at this for more details
尝试这样的事情:
#!/usr/bin/python
import mysql.connector
conn = mysql.connector.connect(host="lll",user="ppp",passwd="ppp",db="ppp")
cursor = conn.cursor()
cursor.execute("SELECT(case when user1 = '1' THEN 'true' ELSE 'false' END) AS IsEmtpy from dt")
print(cursor.fetchall())
try:
if True:
cursor.execute("DELETE FROM dt WHERE user1='1'")
print "DELETE SUCESS"
else:
print "DELETE ERROR"
conn.commit()
except Exception as e:
conn.rollback()
conn.close()