Reputation:
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()
Upvotes: 0
Views: 62
Reputation: 900
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
Upvotes: 2