pymysql在調用MySQL靜態方法的使用

本次只是使用pymysql在調用MySQL 其中書寫了靜態方法的使用,本身比較簡單,框架很容易。但這個是很經典的代碼書寫模式,值得多加練習。

from pymysql import connect

class JD(object):
    def __init__(self):
        self.conn = connect(host='192.168.4.2', port=3306, user='sai',
                       password='123456', database='python',
                       charset='utf8')
        self.cursor = self.conn.cursor()

    def show_all_items(self):
        self.cursor.execute("select * from students")
        for i  in self.cursor.fetchall():
            print(i)

    def show_cates(self):
        sql = "select name from students"
        self.cursor.execute(sql)
        print(self.cursor.fetchall())

    def show_brands(self):
        sql = "select id from students"
        self.cursor.execute(sql)
        print(self.cursor.fetchall())

    def __del__(self):
        self.cursor.close()
        self.conn.close()

    @staticmethod
    def print_menu():
        print("------京東商城--------")
        print("1:所有的商品")
        print("2:所有的商品分類")
        print("3:所有的商品品牌分類")
        return input("輸入對於的序列號:")

    def run(self):
        while True:
            num = self.print_menu()
            if num == "1":
                self.show_all_items()
            elif num == "2":
                self.show_cates()
            elif num == "3":
                self.show_brands()
            else:
                print("輸入有誤")

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

if __name__ == "__main__":
    main()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章