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()
  • 1
    Always show full error message. You have wrong indentions. Don't use `%` to create query - it can be insecure - use `execute(..., arguments)`. – furas Oct 13 '16 at 03:38
  • you forgot last `)` in `int(input(...))` – furas Oct 13 '16 at 03:40
  • OHHHHHHHH ! thank you :D bad forgot –  Oct 13 '16 at 03:43
  • `IndentationError: expected an indented block` –  Oct 13 '16 at 04:10
  • I repeat: Always show full error message. There is not only message but also line with problem, etc. And put error in question. – furas Oct 13 '16 at 04:12
  • this error mean you have wrong indentions - for example:. `try` needs inside lines with indentions but you have without indentions. – furas Oct 13 '16 at 04:15

1 Answers1

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

  • what a `IndentationError: expected an indented block` –  Oct 13 '16 at 04:07
  • 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 – Lucas Batista Gabriel Oct 13 '16 at 04:13
  • And i want to delete multi row together i can ? –  Oct 13 '16 at 04:32
  • check this question: - http://stackoverflow.com/questions/26347412/drop-multiple-columns-pandas - http://stackoverflow.com/questions/5955841/delete-multiple-rows-in-mysql-with-info-from-python-list – Lucas Batista Gabriel Oct 13 '16 at 04:46
  • @UsuchaBootsarakam, always pay attention to your indentation in python - https://www.quora.com/What-does-indentation-mean-in-python – Lucas Batista Gabriel Oct 13 '16 at 04:49