python实现京东商城

用python实现简单版的京东商城

一、数据库准备

1. 商品表

create table goods(
    id int unsigned primary key auto_increment not null,
    name varchar(150) not null,
    cate_id int unsigned not null,
    brand_id int unsigned not null,
    price decimal(10,3) not null default 0,
    is_show bit not null default 1,
    is_saleoff bit not null default 0
);

2. 商品分类表

create table if not exists goods_cates(
    id int unsigned primary key auto_increment,
    name varchar(40) not null
);

3. 商品品牌分类表

create table goods_brands (
    id int unsigned primary key auto_increment,
    name varchar(40) not null
);

二、相应的库准备

应为我的数据库放在云服务器上,因此在准备库的时候需要多加上一个sshtunnel库。(具体原因在上一章)

  • pymysql安装命令:pip install pymysql
  • sshtunnel安装命令:pip install sshtunnel

三、基本骨架

整个代码我们可以利用一个JD类来实现,利用他的__init__方法来构建ssh的连接和mysql的连接,利用__del__方法来释放关闭连接。然后写一个run方法,在此基础上将增删改查四个步骤写入,在将这四个步骤分别细分成每个表即可。话不多说,上代码:

# -*- coding: utf-8 -*-
# @Author  : summer
from sshtunnel import SSHTunnelForwarder
import pymysql


class JD:
    def __init__(self):
        # 初始化,建立ssh连接,然后进行pysql连接
        self.ssh = SSHTunnelForwarder(ssh_address_or_host=(IP, 22),  # 云服务器地址IP和端口port
                                      ssh_username=admin,  # 云服务器登录账号admin
                                      ssh_password=pwd,  # 云服务器登录密码password
                                      # 数据库服务地址ip
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值