Section XII sql injection protection

Import pymysql 

'' ' 
conn.commit () actually writes data to the database 
conn.rollback () cancels the previous operation is sql statement 
' '' 
class JD (): 

    DEF  the __init__ (Self): 
        self.conn = pymysql.connect ( ' localhost ' , ' the root ' , ' ' , ' python_test ' ) 
        self.cursor = self.conn.cursor ()
         # cursor.close () 
        # conn.Close () 
        # the 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('the SELECT * from BRAND_NAME ' ):
             Print (the TEMP)

    DEF add_brand_name (Self): 
        the brandname of = the INPUT ( ' Please enter a brand name you want to add: ' ) 
        SQL = "" " INSERT INTO BRAND_NAME (name) values ("% S ") " "" % the brandname of 
        self.cursor.execute (SQL) 
        self.conn.commit () 

    DEF get_info_goods (Self): 
        the brandname of = the iNPUT ( ' Please enter a product name you are looking for a: ' ) 
        SQL = ' the SELECT * from BRAND_NAME the WHERE name =% S ' 
        self.cursor.execute (SQL, [the brandname of]) 
        Print (self.cursor.fetchall ())


    @staticmethod 
    DEF MUE ():
         Print ( " ...... Jingdong Mall ...... " )
         Print ( ' 1: all the goods ' )
         Print ( ' 2: All merchandise classification ' )
         Print ( ' 3: All brand classification ' )
         Print ( ' 4: Add brand ' )
         Print ( ' 5: Search product details ' )
         Print  (' 0: Close mall ' )
         return the INPUT ( 'Please enter the number of the function with respect to: ' ) 

    DEF RUN (Self):
         the 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 () 
            the else :
                 Print ( ' input is incorrect, please re-enter .... ' ) 
        self.cursor. Close () 
        self.conn.close () 


DEF main (): 
    JD = JD () 
    jd.run () 


IF  the __name__ == ' __main__ ' : 
    main ()

 

Guess you like

Origin www.cnblogs.com/kogmaw/p/12405822.html