如果值为true但null不起作用

时间:2016-10-13 03:33:24

标签: python mysql

我在mysql中创建了程序输入编号和删除数据。但运行程序错误然后报告sql1语法错误\ i更改为真

#!/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 个答案:

答案 0 :(得分:2)

num = int(输入(“InputNumber:”)class Candidate::JobsController < Candidate::BaseController end &lt; - 不要忘记关闭它

我不确定),我总是看到%i用于整数,%d用于字符串

但是,您的查询中也存在一个问题,SQL Injection

为避免这种情况,请尝试这样的事情

%s

您可以查看question mark herehere,了解绑定价值的原因