zoukankan      html  css  js  c++  java
  • 第十二节 sql注入防护

    import pymysql
    
    '''
    conn.commit() 真正将数据写入数据库
    conn.rollback() 取消前面是sql语句操作
    '''
    class JD():
    
        def __init__(self):
            self.conn = pymysql.connect('localhost','root','','python_test')
            self.cursor = self.conn.cursor()
            # cursor.close()
            # conn.close()
            # cursor.execute('select * from tdb_goods')
        def sql_exe(self,sql):
            self.cursor.execute(sql)
            ret = self.cursor.fetchall()
            return ret
    
        def show_all_item(self):
            for temp in self.sql_exe('select * from tdb_goods'):
                print(temp)
    
        def show_goods_cate(self):
            for temp in self.sql_exe('select * from goods_cate'):
                print(temp)
    
        def show_brand_name(self):
            for temp in self.sql_exe('select * from brand_name'):
                print(temp)
    
        def add_brand_name(self):
            brandname = input('请输入你要添加的品牌名称:')
            sql = """insert into brand_name (name) values ("%s")""" % brandname
            self.cursor.execute(sql)
            self.conn.commit()
    
        def get_info_goods(self):
            brandname = input('请输入你查找的的商品名称:')
            sql = 'select * from brand_name where name=%s'
            self.cursor.execute(sql, [brandname])
            print(self.cursor.fetchall())
    
    
        @staticmethod
        def mue():
            print("......京东商城......")
            print('1:所有的商品')
            print('2:所有商品的分类')
            print('3:所有的商品品牌分类')
            print('4:添加商品品牌')
            print('5:搜索商品详情')
            print('0:关闭商城')
            return input('请输入功能相对于的序号:')
    
        def run(self):
            while True:
                num = self.mue()
                if num == '1':
                    self.show_all_item()
                elif num == '2':
                    self.show_goods_cate()
                elif num == '3':
                    self.show_brand_name()
                elif num == '0':
                    break
                elif num == '4':
                    self.add_brand_name()
                elif num == '5':
                    self.get_info_goods()
                else:
                    print('输入有误,请重新输入....')
            self.cursor.close()
            self.conn.close()
    
    
    def main():
        jd = JD()
        jd.run()
    
    
    if __name__ == '__main__':
        main()
  • 相关阅读:
    CTFHub_技能树_文件上传
    QT入门-重载的信号槽
    QT入门-自定义信号
    C++: xx does not name a type报错
    HDU1166 敌兵布阵
    洛谷P2574 XOR的艺术(线段树)
    P3373 【模板】线段树 2(板子好题)
    SP1716 GSS3
    QT入门-自定义槽函数
    Educational Codeforces Round 87 (Rated for Div. 2) D. Multiset(树状数组/好题)
  • 原文地址:https://www.cnblogs.com/kogmaw/p/12405822.html
Copyright © 2011-2022 走看看