Usucha Bootsarakam Usucha Bootsarakam - 1 month ago 15
MySQL Question

Python select data return boolean true-false and DELETE value true

I made a program transaction.and if error,it should report position error. NOW I want to select value TRUE and DELETE value 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()


result :

File "./splinsert.py", line 13
if true:
^
IndentationError: expected an indented block

Answer

This error is related to inconsistent indentation. Python relies on indentation to determine when "code blocks" start and stop. Take a look at this for more details.

Try something like this:

#!/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()