美文网首页
京东python交互代码

京东python交互代码

作者: 星河入怀fd | 来源:发表于2019-07-17 17:32 被阅读0次

from pymysql import connect

class JD(object):

    def __init__(self):

        self.is_login = False #默认没有登录

        # 创建Connection连接

        self.conn = connect(host='localhost',port=3306,user='root',password='123456',database='jd',charset='utf8')

        # 获得Cursor对象

        self.cursor = self.conn.cursor()

    def __del__(self):

        # 关闭Cursor对象

        self.cursor.close()

        # 关闭Connection对象

        self.conn.close()

    def register(self):

        username = input("请输入您注册的用户名:")

        address = input("请输入地址:")

        tel = input("请输入您的手机号:")

        pwd = input("请输入您注册的密码:")

        sql = "insert into customers values(0,%s,%s,%s,password(%s));"#password 包一下的意思加密

        self.cursor.execute(sql,[username,address,tel,pwd])

        self.conn.commit()

    def login(self):

        username=input("请输入用户名:")

        pwd = input("请输入您的密码:")

        sql = "select id from customers where name=%s and passwd=password(%s)"

        self.cursor.execute(sql,[username,pwd])

        self.uid = self.cursor.fetchone()

        if self.uid:

            self.uid = self.uid[0]

            self.is_login = True

        return self.is_login

    def show_all_items(self):

        #显示所有商品

        sql = "select * from goods;"

        self.execute_sql(sql)

    def show_cates(self):

        sql = "select name from goods;"

        self.execute_sql(sql)

    def show_brands(self):

        sql = "select name from goods_brands;"

        self.execute_sql(sql) 

    def add_cates(self):

        item_name = input("请输入新商品分类的名称:")

        sql = """insert into goods_cates (name) values("%s");""" %item_name

        self.cursor.execute(sql)

        self.conn.commit() 

    def change_cates(self):

        old_name = input("请输入您要更改的商品分类的名称:")

        new_name = input("请输入您要更改的新商品名称:")

        sql = """update goods_cates set name=("%s") where name=("%s");"""%(new_name,old_name)

        self.cursor.execute(sql)

        self.conn.commit()

    def delete_cates(self):

        del_id = int(input("请输入您要删除的id号:"))

        sql = """delete from goods_cates where id=("%d");"""%del_id

        self.cursor.execute(sql)

        self.conn.commit()

    def get_info_by_name(self):

        find_name = input("请输入要查询的商品的名字:")

        sql ="select * from goods where name=%s;"

        self.cursor.execute(sql,[find_name])

        print(self.cursor.fetchall())

    def create_order(self):

        pid = input("请输入商品的编号:")

        num = input('请输入商品的数量:')

        sql ="select * from goods where id=%s;"

        result = self.cursor.execute(sql,[pid])

        if result:

            sql = "insert into orders values(0,now(),%s);"

            self.cursor.execute(sql,[self.uid])

            sql = "insert into order_detail values(0,%s,%s,%s);"

            self.cursor.execute(sql,[self.cursor.lastrowid,pid,num])

            self.conn.commit()

            print("下单成功")

        else:

            print('你要购买的商品已下架')

    @staticmethod

    def sys_menu():

        print("-----京东-----")

        print("1 >>> 注册")

        print("2 >>> 登录")

        print("3 >>> 退出")

        return input("请输入功能对应的序号:")

    @staticmethod

    def print_menu():

        print("")

        print("-----系统菜单-----")

        print("1:所有的商品")   

        print("2:所有的商品分类") 

        print("3:所有的商品品牌")

        print("4:添加商品分类")

        print("5:更改商品分类")

        print("6:删除商品分类")

        print("7:查找商品")

        print("8:下单")

        print("其他任意键:退出") 

        return input("请输入功能对应的序号:")

    def run(self):

        while True:

            num = self.sys_menu()

            if num == "1":

                #注册

                self.register()

            elif num == "2":

                #登录

                if self.login():

                    print("登录成功")

                    break

                else:

                    print("用户名或密码有误,请重新输入......")

            elif num== "3":

                break

            else:

                print('输入有误,请重新输入')

        if self.is_login:#如果登录成功,显示系统菜单

            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()

                elif num == "4":

                    #添加商品分类

                    self.add_cates()

                elif num == "5":

                    #更改商品分类

                    self.change_cates()

                elif num == "6":

                    #删除商品分类

                    self.del_cates()

                elif num == "7":

                    #根据名字查询商品

                    self.get_info_by_name()

                elif num == "8":

                    # 下单

                    self.create_order()

                else:

                    print("----再见----")

                    break

            else:

                print("----再见----")

def main():

    #1.创建一个京东商城对象

    jd = JD()

    #2.调用这个盾想的run方法,让其运行。

    jd.run()

if __name__ == '__main__':

    main()

相关文章

  • 京东python交互代码

    from pymysql import connect class JD(object): def __ini...

  • 学习网址

    Python开源爬虫项目代码:抓取淘宝、京东、QQ、知网数据 scrapy_jingdong[9]- 京东爬虫。基...

  • Python初学笔记(感觉语法层面确实很好上手,后续有黑科技再补

    Python 是交互式语言 Python 中冒号(:)之后缩进的语句为代码块 Python中"XX" ,'XX' ...

  • python学习(廖雪峰的官方网站)

    廖雪峰的官方网站。 请注意区分命令行模式和Python交互模式。 1在Python交互式模式下,可以直接输入代码,...

  • python基础

    1.Python应用场景 1.1交互式python开发 1.2程序运行原理 1.3python文件代码结构示意图 ...

  • python模块

    Python 中的模块 模块是一个包含 Python 代码的 .py 文件模块既可以被导入到 Python 的交互...

  • 2018-04-07 MySQL与Python交互

    与Python交互需要先安装pymysql 代码为: sudo pip3 install pymysql 连...

  • 基础语法

    1. 交互式编程 交互式编程不需要创建脚本文件,是通过 Python 解释器的交互模式进来编写代码。 linux上...

  • python

    Python的个是非常严格,每行代码前面都不要有空格缩进 交互式运行Python:在终端中输入Python进入官方...

  • 1.协议栈、编解码、IP

    1.参考书及源代码 所用参考书为Python网络编程(中文第三版),京东有售。源代码放在了源码-Python网络编...

网友评论

      本文标题:京东python交互代码

      本文链接:https://www.haomeiwen.com/subject/fheclctx.html