pymysql 防止sql注入案例

from pymysql import connect


def main():
"""sql演示"""
# 1.输入一个语句,根据id展示相应的内容

id = input("请输入查询的id")

# 2.执行sql语句
# 创建Connection连接
conn = connect(host='localhost', port=3306, database='jing_dong', user='root', password='mysql', charset='utf8')
# 获得Cursor对象
cs1 = conn.cursor()

# sql = """select * from goods where id = %s;""" % id 以后sql语句 千万不要直接 使用字符串拼接
sql = """select * from goods where id = %s;""" # 以后sql语句 千万不要直接 使用字符串拼接

# print(sql)
cs1.execute(sql,(id,)) # 以后参数直接在这里进行传递


data = cs1.fetchall()

cs1.close()
conn.close()

# 3.打印结果2
for temp in data:
print(temp)


if __name__ == '__main__':
main()

猜你喜欢

转载自www.cnblogs.com/wjun0/p/11515444.html