加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python操作sql

发布时间:2020-12-20 10:57:47 所属栏目:Python 来源:网络整理
导读:from pymysql import connectclass JD(object): def __init__(self): # 创建connect连接 self.conn = connect(host='127.0.0.1',port=3306,user='root',password='123456',database='jing_dong',charset='utf8') # 获得cursor对象 self.cursor = self.conn.c
from pymysql import connect


class JD(object):
    def __init__(self):
        # 创建connect连接
        self.conn = connect(host='127.0.0.1',port=3306,user='root',password='123456',database='jing_dong',charset='utf8')
        # 获得cursor对象
        self.cursor = self.conn.cursor()

    def __del__(self):
        # 关闭corser对象
        self.cursor.close()
        self.conn.close()

    def execute_sql(self,sql):
        self.cursor.execute(sql)
        for temp in self.cursor.fetchall():
            print(temp)

    def show_all_item(self):
        """显示所有商品"""
        sql = 'SELECT * FROM goods'
        self.execute_sql(sql)

    def show_cates(self):
        """显示所有商品"""
        sql = 'SELECT name FROM goods_cates'
        self.execute_sql(sql)

    def show_brand(self):
        """显示所有的商品的品牌分类"""
        sql = 'SELECT name FROM goods_brand'
        self.execute_sql(sql)

    @staticmethod
    def print_menu():
        print('-----京东-----')
        print('1.所有的商品')
        print('2.所有的商品的分类')
        print('3.所有的商品的品牌分类')
        return input('请输入功能对应的序号: ')

    def run(self):
        while True:
            op = self.print_menu()
            if op == '1':
                # 查询所有商品
                self.show_all_item()
            elif op == '2':
                # 查询所有的商品的分类
                self.show_cates()
            elif op == '3':
                # 查询所有的商品的品牌分类
                self.show_brand()
            else:
                print('输入有误,请重新输入...')


def main():
    # 1.创建一个JD对象
    jd = JD()
    # 2.调用JD对象的run方法
    jd.run()


if __name__ == '__main__':
    main()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读