0

I made program is input number and delete data in mysql. but run program error then report sql1 Syntax Error\i change true

#!/usr/bin/python import mysql.connector conn = mysql.connector.connect(host="",user="",passwd="",db="") cursor = conn.cursor() try: num = int(input("InputNumber 1-10 : ")) if num <= 10: if num == null: //if null print false sql1 = "SELECT user1 FROM dt WHERE user1 = '%d' " %(num) cursor.execute(sql1) data = cursor.fetchall() print(data[0]) sqlde = "DELETE FROM dt WHERE user1 = '%d' " %(num) cursor.execute(sqlde, (num)) print "DELETE SUCESS" conn.commit() else: print "Data Empty" except: conn.rollback() conn.close() 
6
  • 1
    Always show full error message. You have wrong indentions. Don't use % to create query - it can be insecure - use execute(..., arguments). Commented Oct 13, 2016 at 3:38
  • you forgot last ) in int(input(...)) Commented Oct 13, 2016 at 3:40
  • OHHHHHHHH ! thank you :D bad forgot Commented Oct 13, 2016 at 3:43
  • IndentationError: expected an indented block Commented Oct 13, 2016 at 4:10
  • I repeat: Always show full error message. There is not only message but also line with problem, etc. And put error in question. Commented Oct 13, 2016 at 4:12

1 Answer 1

2

num = int(input("InputNumber: ")) <- don't forguet to close it

I'm not sure about the %i, I always see using %d for Integer and %s to strings

But, you also have one problem into your query, SQL Injection

So to avoid this, try something like this

sql1 = "DELETE FROM dt WHERE user1 = ?" try: cursor.execute(sql1, (num)) print "DELETE SUCECC" conn.commit() except: conn.rollback() print "ERROR DELETE" 

you can check about question mark here or here and understand why you should bind your values

Sign up to request clarification or add additional context in comments.

5 Comments

what a IndentationError: expected an indented block
This is because you are mixing Spaces and tabs. Decide just one kind of identation and use-it, or just spaces, or just tabs, my recomendation use just spaces. Most editors have an option for automatically converting tabs to spaces. If your editor has this option, turn it on
And i want to delete multi row together i can ?
@UsuchaBootsarakam, always pay attention to your indentation in python - quora.com/What-does-indentation-mean-in-python