日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

京东商城(mysql+python)

發(fā)布時(shí)間:2023/12/16 数据库 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 京东商城(mysql+python) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

前言

1>項(xiàng)目來自網(wǎng)上一個(gè)教學(xué)視頻,小白博主對(duì)其進(jìn)行了小小的優(yōu)化,實(shí)現(xiàn)了相關(guān)密碼加密,下訂單等相關(guān)操作。

一:項(xiàng)目準(zhǔn)備

1>數(shù)據(jù)庫準(zhǔn)備

這是我為該項(xiàng)目所建的表:

表內(nèi)數(shù)據(jù)(因?yàn)椴┲饔行┧饺诵畔⒕筒还_了,這里給出表的結(jié)構(gòu):

其中的customer存儲(chǔ)用戶信息(包含姓名、住址、電話、密碼等)。

good_bands存儲(chǔ)商品的品牌。

good_cate存儲(chǔ)商品的種類。

goods存儲(chǔ)商品的具體信息(名稱、價(jià)格、是否售空、以及庫存數(shù)量)。

order_information存儲(chǔ)訂單的訂單號(hào),商品以及購買數(shù)量。

orders存儲(chǔ)顧客的購買時(shí)間和昵稱。

2>python相關(guān)類庫的準(zhǔn)備:

該項(xiàng)目主要使用了python的pymyql(用于連接mysql)、time(登錄時(shí)間以及訂單號(hào)的生成)、hashlib(密碼的加密)

第三方庫的下載可以使用pip指令也可以在pycharm中直接在python packages點(diǎn)擊搜索進(jìn)行下載。

pip install hashlib

二:項(xiàng)目主要功能介紹

1>數(shù)據(jù)庫基本連接

def __init__(self):# 產(chǎn)生連接self.conner = connect(host="localhost", port=3306, user='root', password="liu20020822",database="python_01", charset='utf8')# 獲得Cursor對(duì)象self.cursor = self.conner.cursor()num = Falsename = ""passwd = ""def __del__(self):self.cursor.close()self.conner.close()

每次運(yùn)行時(shí)均會(huì)對(duì)用戶昵稱和密碼進(jìn)行初始化,其中num用于判斷是否進(jìn)行了登錄或者注冊(cè)等操作。

2>顧客注冊(cè)以及登錄

# 進(jìn)行注冊(cè)def get_index_count(self):print("--------登錄賬號(hào)--------")self.name = str(input("請(qǐng)輸入你的賬號(hào):"))self.passwd = str(input("請(qǐng)輸入你的密碼:"))# 利用類庫進(jìn)行加密self.passwd = self.passwd.strip()md5 = hashlib.md5()md5.update(self.passwd.encode(encoding='utf-8'))self.passwd = md5.hexdigest()sql = ('select * from customer where name="%s" and passwd="%s";' % (self.name, self.passwd))self.cursor.execute(sql)change = str(self.cursor.fetchone())+"1"if change == 'None1':print("登錄失敗、請(qǐng)核對(duì)賬號(hào)和密碼或點(diǎn)擊7進(jìn)行注冊(cè)")else:# 下面這串代碼無意義本想刪的,但是懶得改了,可以直接用成功替代sql_user_frequency = "select user_frequency from customer where name ='%s';" % self.nameself.cursor.execute(sql_user_frequency)good_number_list = list(self.cursor.fetchall())good_number_tuple = good_number_list[0]sql_goods_update_number = ("update customer set user_frequency = %d where name='%s';"% (good_number_tuple[0] + 1, self.name))self.cursor.execute(sql_goods_update_number)print("賬號(hào)登錄成功")self.num = Trueself.conner.commit()# 進(jìn)行顧客的注冊(cè)def get_registered_count(self):print("--------注冊(cè)賬號(hào)--------")while True:self.name = str(input("請(qǐng)輸入你的昵稱:"))sql_name_search = ('select name from customer where name="%s"' % self.name)self.cursor.execute(sql_name_search)if self.cursor.fetchall():print("此用戶名那已經(jīng)存在,請(qǐng)重新輸入")else:breakaddress = str(input("請(qǐng)輸入你的家庭地址:"))telephone = str(input("請(qǐng)輸入你的電話:"))self.passwd = str(input("請(qǐng)輸入你的密碼:"))# 密碼同樣需要加密 以免在登錄中進(jìn)行密碼比對(duì)中出錯(cuò)(可以寫成一個(gè)函數(shù)的,有點(diǎn)懶)self.passwd = self.passwd.strip()md5 = hashlib.md5()md5.update(self.passwd.encode(encoding='utf-8'))self.passwd = md5.hexdigest()sql = "select table_name from information_schema.tables where table_name ='customer';"if not self.cursor.execute(sql):sql = """create table if not exists Customer(id int unsigned primary key not null auto_increment,name varchar(10) not null default "老王",address varchar(30) not null,telephone varchar(11) not null,passwd varchar(30) not null);"""self.cursor.execute(sql)sql = ('insert into Customer values(0,"%s","%s",%s,"%s");' % (self.name, address, telephone, self.passwd))self.cursor.execute(sql)print("表單注冊(cè)成功")else:sql = ('insert into Customer values( 0, "%s", "%s", %s, "%s", 0 );'% (self.name, address, telephone, self.passwd))self.cursor.execute(sql)print("成功注冊(cè)")# 將num屬性進(jìn)行更改self.num = Trueself.conner.commit()

3>商品品牌等展示

# 這些是表中數(shù)據(jù)的一些查詢,隨手寫寫就好def show_all_items(self):sql = 'select name from goods;'self.execute_sql(sql)def show_all_bands(self):sql = 'select name from good_cate;'self.execute_sql(sql)def show_all_type(self):sql = 'select band from good_bands;'self.execute_sql(sql)

4>下訂單

def get_info_sold(self):print("--------開始下訂單--------")# 下訂單的時(shí)間now_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())customer_name = self.name# 訂單號(hào),用時(shí)間數(shù)據(jù)代替,第一個(gè)使用年是為了防止重復(fù)(寫文章的時(shí)候才改的,,)order_number = str(time.strftime('%Y%M%S', time.localtime(time.time()))+ str(time.time()).replace('.', '')[-7:])good = input("請(qǐng)輸入要購買的商品:")number = eval(input("請(qǐng)輸入要購買的數(shù)量:"))# 首先判斷商品是否存在在goods表中是否存在該商品sql = "select name from goods where name='%s';" % goodif self.cursor.execute(sql):# 查詢表是否存在,當(dāng)時(shí)傻逼了,其實(shí)都應(yīng)該表是提前建好的,省去這個(gè)步驟sql = "select table_name from information_schema.tables where table_name ='orders';"if self.cursor.execute(sql):sql_good_number = "select number from goods where name ='%s';" % goodself.cursor.execute(sql_good_number)good_number_list = list(self.cursor.fetchall())good_number_tuple = good_number_list[0]if good_number_tuple[0]-number > 0:sql_goods_update_number = ("update goods set number=%d where name='%s';"% (good_number_tuple[0] - number, good))sql_good_insert = ("insert into Orders values(0,'%s','%s');" % (now_time, customer_name))self.cursor.execute(sql_good_insert)self.cursor.execute(sql_goods_update_number)sql_good_information_insert = ("insert into Order_Information values(0,'%s','%s','%s');"% (order_number, good, number))self.cursor.execute(sql_good_information_insert)print("購買成功")else:print("該商品貨物不足,只有%d件了" % (good_number_tuple[0]))else:# 這里就是建表sql_good_create = """create table if not exists Orders(id int unsigned not null auto_increment primary key,date_time datetime not null,customer varchar(30) not null)"""self.cursor.execute(sql_good_create)sql_good_information_create = """create table if not exists Order_Information(id int unsigned primary key not null auto_increment,order_number varchar(30) not null default "8888888",goods varchar(15) not null,number int unsigned not null default 0);"""self.cursor.execute(sql_good_information_create)sql_good_insert = ("insert into Orders values(0,'%s','%s');" % (now_time, customer_name))self.cursor.execute(sql_good_insert)sql_good_information_insert = ("insert into Order_Information values(0,'%s','%s','%s');"% (order_number, good, number))self.cursor.execute(sql_good_information_insert)print("該表成功創(chuàng)建")self.conner.commit()else:print("商品不存在")

5>主函數(shù)調(diào)用

def run(self):while True:print("--------京東商城--------\n\n"+"--------1.0 所有的商品名稱\n"+"--------2.0 所有的商品品牌\n"+ "--------3.0 所有的商品屬性\n"+"--------4.0 添加一個(gè)商品 ")print("--------5.0 根據(jù)名字查詢商品\n"+"--------6.0 登錄賬號(hào)\n"+"--------7.0 注冊(cè)賬號(hào)\n"+"--------8.0 下訂單\n"+"--------0 退出")select = eval(input("請(qǐng)輸入功能對(duì)應(yīng)選項(xiàng):\n"))if select == 1: # 查詢所有的商品名稱if self.num:self.show_all_items()else:print("請(qǐng)重新登錄")elif select == 2: # 查詢你所有商品品牌if self.num:self.show_all_bands()else:print("請(qǐng)重新登錄")elif select == 3: # 查詢你所有的商品屬性if self.num:self.show_all_type()else:print("請(qǐng)重新登錄")elif select == 4:if self.num:self.get_add_type()else:print("請(qǐng)重新登錄")elif select == 5:if self.num:self.get_info_by_name()else:print("請(qǐng)重新登錄")elif select == 6:self.get_index_count()elif select == 7:self.get_registered_count()elif select == 8:if self.num:self.get_info_sold()else:print("請(qǐng)重新登錄")elif select == 0:breakelse:print("輸入錯(cuò)誤 請(qǐng)重新輸入 或重新登錄")

我使用了簡(jiǎn)單的選擇,字典也可以,有點(diǎn)冗余了。

三:完整源碼附錄:

from pymysql import * import time import hashlibclass Good(object):def __init__(self):# 產(chǎn)生連接self.conner = connect(host="localhost", port=3306, user='root', password="liu20020822",database="python_01", charset='utf8')# 獲得Cursor對(duì)象self.cursor = self.conner.cursor()num = Falsename = ""passwd = ""def __del__(self):self.cursor.close()self.conner.close()def execute_sql(self, sql):self.cursor.execute(sql)for temp in self.cursor.fetchall():print(temp)def show_all_items(self):sql = 'select name from goods;'self.execute_sql(sql)def show_all_bands(self):sql = 'select name from good_cate;'self.execute_sql(sql)def show_all_type(self):sql = 'select band from good_bands;'self.execute_sql(sql)def get_add_type(self):item = input("輸入待加入的商品名稱:")cate_id = eval(input("請(qǐng)輸入品牌id:(數(shù)字)"))brand_id = input("請(qǐng)輸入品牌屬性id:")price = eval(input("請(qǐng)輸入價(jià)格:"))number = eval(input("請(qǐng)輸入代售數(shù)量:"))sql = ("""insert into goods values(0,"%s","%d","%s","%d",default,default,"%d")"""% (item, cate_id, brand_id, price, number))self.cursor.execute(sql)self.conner.commit()def get_info_by_name(self):item = input("輸入待搜查的商品品牌名稱:")sql = "select * from goods where name=%s;"self.cursor.execute(sql, [item]) # 執(zhí)行sql語句、同時(shí)防止sql語句注入print(self.cursor.fetchall())def get_index_count(self):print("--------登錄賬號(hào)--------")self.name = str(input("請(qǐng)輸入你的賬號(hào):"))self.passwd = str(input("請(qǐng)輸入你的密碼:"))self.passwd = self.passwd.strip()md5 = hashlib.md5()md5.update(self.passwd.encode(encoding='utf-8'))self.passwd = md5.hexdigest()sql = ('select * from customer where name="%s" and passwd="%s";' % (self.name, self.passwd))self.cursor.execute(sql)change = str(self.cursor.fetchone())+"1"if change == 'None1':print("登錄失敗、請(qǐng)核對(duì)賬號(hào)和密碼或點(diǎn)擊7進(jìn)行注冊(cè)")else:sql_user_frequency = "select user_frequency from customer where name ='%s';" % self.nameself.cursor.execute(sql_user_frequency)good_number_list = list(self.cursor.fetchall())good_number_tuple = good_number_list[0]sql_goods_update_number = ("update customer set user_frequency = %d where name='%s';"% (good_number_tuple[0] + 1, self.name))self.cursor.execute(sql_goods_update_number)print("賬號(hào)登錄成功")self.num = Trueself.conner.commit()def get_registered_count(self):# 如果不存在創(chuàng)建一個(gè)新表、填充顧客的id(不進(jìn)行手動(dòng)輸入自動(dòng)進(jìn)行填充) 呢稱 住址 電話 密碼# 如果存在則只有填充不新建print("--------注冊(cè)賬號(hào)--------")while True:self.name = str(input("請(qǐng)輸入你的昵稱:"))sql_name_search = ('select name from customer where name="%s"' % self.name)self.cursor.execute(sql_name_search)if self.cursor.fetchall():print("此用戶名那已經(jīng)存在,請(qǐng)重新輸入")else:breakaddress = str(input("請(qǐng)輸入你的家庭地址:"))telephone = str(input("請(qǐng)輸入你的電話:"))self.passwd = str(input("請(qǐng)輸入你的密碼:"))self.passwd = self.passwd.strip()md5 = hashlib.md5()md5.update(self.passwd.encode(encoding='utf-8'))self.passwd = md5.hexdigest()sql = "select table_name from information_schema.tables where table_name ='customer';"if not self.cursor.execute(sql):sql = """create table if not exists Customer(id int unsigned primary key not null auto_increment,name varchar(10) not null default "老王",address varchar(30) not null,telephone varchar(11) not null,passwd varchar(30) not null);"""self.cursor.execute(sql)sql = ('insert into Customer values(0,"%s","%s",%s,"%s");' % (self.name, address, telephone, self.passwd))self.cursor.execute(sql)print("表單注冊(cè)成功")else:sql = ('insert into Customer values( 0, "%s", "%s", %s, "%s", 0 );'% (self.name, address, telephone, self.passwd))self.cursor.execute(sql)print("成功注冊(cè)")self.num = Trueself.conner.commit()def get_info_sold(self):print("--------開始下訂單--------")now_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())customer_name = self.nameorder_number = str(time.strftime('%H%M%S', time.localtime(time.time()))+ str(time.time()).replace('.', '')[-7:])# 首次創(chuàng)建訂單表和訂單詳情表、兩表存在關(guān)聯(lián)同時(shí)存在或者消失 id 訂單號(hào) 時(shí)間 顧客 均自動(dòng)生成、選填數(shù)量和商品good = input("請(qǐng)輸入要購買的商品:")number = eval(input("請(qǐng)輸入要購買的數(shù)量:"))# 首先判斷商品是否存在在goods表中是否存在該商品sql = "select name from goods where name='%s';" % goodif self.cursor.execute(sql):sql = "select table_name from information_schema.tables where table_name ='orders';"if self.cursor.execute(sql):sql_good_number = "select number from goods where name ='%s';" % goodself.cursor.execute(sql_good_number)good_number_list = list(self.cursor.fetchall())good_number_tuple = good_number_list[0]if good_number_tuple[0]-number > 0:sql_goods_update_number = ("update goods set number=%d where name='%s';"% (good_number_tuple[0] - number, good))sql_good_insert = ("insert into Orders values(0,'%s','%s');" % (now_time, customer_name))self.cursor.execute(sql_good_insert)self.cursor.execute(sql_goods_update_number)sql_good_information_insert = ("insert into Order_Information values(0,'%s','%s','%s');"% (order_number, good, number))self.cursor.execute(sql_good_information_insert)print("購買成功")else:print("該商品貨物不足,只有%d件了" % (good_number_tuple[0]))else:sql_good_create = """create table if not exists Orders(id int unsigned not null auto_increment primary key,date_time datetime not null,customer varchar(30) not null)"""self.cursor.execute(sql_good_create)sql_good_information_create = """create table if not exists Order_Information(id int unsigned primary key not null auto_increment,order_number varchar(30) not null default "8888888",goods varchar(15) not null,number int unsigned not null default 0);"""self.cursor.execute(sql_good_information_create)sql_good_insert = ("insert into Orders values(0,'%s','%s');" % (now_time, customer_name))self.cursor.execute(sql_good_insert)sql_good_information_insert = ("insert into Order_Information values(0,'%s','%s','%s');"% (order_number, good, number))self.cursor.execute(sql_good_information_insert)print("該表成功創(chuàng)建")self.conner.commit()else:print("商品不存在")def run(self):while True:print("--------京東商城--------\n\n"+"--------1.0 所有的商品名稱\n"+"--------2.0 所有的商品品牌\n"+ "--------3.0 所有的商品屬性\n"+"--------4.0 添加一個(gè)商品 ")print("--------5.0 根據(jù)名字查詢商品\n"+"--------6.0 登錄賬號(hào)\n"+"--------7.0 注冊(cè)賬號(hào)\n"+"--------8.0 下訂單\n"+"--------0 退出")select = eval(input("請(qǐng)輸入功能對(duì)應(yīng)選項(xiàng):\n"))if select == 1: # 查詢所有的商品名稱if self.num:self.show_all_items()else:print("請(qǐng)重新登錄")elif select == 2: # 查詢你所有商品品牌if self.num:self.show_all_bands()else:print("請(qǐng)重新登錄")elif select == 3: # 查詢你所有的商品屬性if self.num:self.show_all_type()else:print("請(qǐng)重新登錄")elif select == 4:if self.num:self.get_add_type()else:print("請(qǐng)重新登錄")elif select == 5:if self.num:self.get_info_by_name()else:print("請(qǐng)重新登錄")elif select == 6:self.get_index_count()elif select == 7:self.get_registered_count()elif select == 8:if self.num:self.get_info_sold()else:print("請(qǐng)重新登錄")elif select == 0:breakelse:print("輸入錯(cuò)誤 請(qǐng)重新輸入 或重新登錄")def main():client = Good()client.run()if __name__ == '__main__':main()

結(jié)束語:博主的第一篇,以后還會(huì)繼續(xù)寫作,如果上述項(xiàng)目有不同見解的,歡迎大家在評(píng)論區(qū)或者私聊我。

總結(jié)

以上是生活随笔為你收集整理的京东商城(mysql+python)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。