查詢數(shù)據(jù)、增刪改粗截、添加分類惋耙、sql注入

from pymysqlimport connect

class WN(object):

def __init__(self):

self.is_login =False

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

# 獲取cursor對象

? ? ? ? self.cursor =self.conn.cursor()

def __del__(self):

# 關(guān)閉cursor對象

? ? ? ? self.cursor.close()

self.conn.close()

def execute_sql(self,sql):

self.cursor.execute(sql)

for tempin self.cursor.fetchall():

print(temp)

def show__all__items(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_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 update_brands(self):

id=0

? ? ? ? while True:

try:

id =int(input('輸入要修改的商品分類的id:'))

except Exception as ret:

print('輸入正確的id編號!')

else:

break

? ? ? ? item_name =input('輸入新商品分類的名稱:')

sql ="update goods_brands set name=%s where id=%s"

? ? ? ? self.cursor.execute(sql,[item_name,id])

self.conn.commit()

def update_cates(self):# 修改

? ? ? ? id=0

? ? ? ? while True:

try:

id =int(input('輸入要修改的商品分類的id:'))

except Exception as ret:

print('輸入正確的id編號!')

else:

break

? ? ? ? item_name =input('輸入新商品分類的名稱:')

sql ="update goods_cates set name=%s where id=%s"

? ? ? ? self.cursor.execute(sql,[item_name,id])

self.conn.commit()

def delete_cates(self):# 刪除

? ? ? ? id=0

? ? ? ? while True:

try:

id=input('輸入要刪除的商品分類的id:')

except Exception as ret:

print('輸入正確的ID編號')

else:

break

? ? ? ? isdel =input('你確定要刪除編號為%s的商品分類嗎绽榛,按y確認(rèn)遥金,其他鍵返回主菜單:'%id)

if isdel =='y':

pass

? ? def register(self):# 注冊

? ? ? ? username =input('請輸入用戶名:')

pwd =input('輸入密碼:')

addr =input('請輸入地址:')

tel =input('輸入電話:')

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

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

self.conn.commit()

print('注冊成功了!')

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 create_order(self):

pid =input('輸入商品的編號')

num =input('輸入商品的數(shù)量')

sql ="select *from goods where id=%s and is_show=1 and is_saleoff=1;"

? ? ? ? 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('你要購買的商品已經(jīng)下架')

@staticmethod

? ? def sys_menu():

print('-------商品--------')

print('1.>>>注冊')

print('2.>>>登錄')

print('3.>>>退出')

return input('輸入功能序號')

def get_info_by_name(self):

find_name =input('請輸入你要查詢的商品的名字:')

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

# print('-->%s<--'%sql)

# self.execute_sql(sql)

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

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

print(self.cursor.fetchall())

@staticmethod

? ? def print_menu():

print('-------蝸牛--------')

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

print('2:所有商品分類')

print('3:所有商品品牌分類')

print('4:添加商品分類')

print('7:根據(jù)名字查詢商品')

return input('請輸入功能對應(yīng)的序號:')

def run(self):

while True:

num =self.sys_menu()

if num =='1':

self.register()

elif num =='2':

if self.login():

print('登錄成功稿械!')

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 =='7':

# 根據(jù)名字查詢商品

? ? ? ? ? ? ? ? ? ? self.get_info_by_name()

else:

print('輸入有誤厢呵,請重新輸入.....')

def main():

#? ?? 1、創(chuàng)建一個商城對象

? ? wn = WN()

#? ?? 2傀顾、調(diào)用這個對象run方法襟铭,讓其運行

? ? wn.run()

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市短曾,隨后出現(xiàn)的幾起案子寒砖,更是在濱河造成了極大的恐慌,老刑警劉巖嫉拐,帶你破解...
    沈念sama閱讀 219,039評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件哩都,死亡現(xiàn)場離奇詭異,居然都是意外死亡婉徘,警方通過查閱死者的電腦和手機(jī)漠嵌,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來盖呼,“玉大人儒鹿,你說我怎么就攤上這事〖肝睿” “怎么了约炎?”我有些...
    開封第一講書人閱讀 165,417評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長锌仅。 經(jīng)常有香客問我章钾,道長,這世上最難降的妖魔是什么热芹? 我笑而不...
    開封第一講書人閱讀 58,868評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮惨撇,結(jié)果婚禮上伊脓,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好报腔,可當(dāng)我...
    茶點故事閱讀 67,892評論 6 392
  • 文/花漫 我一把揭開白布株搔。 她就那樣靜靜地躺著,像睡著了一般纯蛾。 火紅的嫁衣襯著肌膚如雪纤房。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,692評論 1 305
  • 那天翻诉,我揣著相機(jī)與錄音炮姨,去河邊找鬼。 笑死碰煌,一個胖子當(dāng)著我的面吹牛舒岸,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播芦圾,決...
    沈念sama閱讀 40,416評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼蛾派,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了个少?” 一聲冷哼從身側(cè)響起洪乍,我...
    開封第一講書人閱讀 39,326評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎夜焦,沒想到半個月后典尾,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,782評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡糊探,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,957評論 3 337
  • 正文 我和宋清朗相戀三年钾埂,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片科平。...
    茶點故事閱讀 40,102評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡褥紫,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出瞪慧,到底是詐尸還是另有隱情髓考,我是刑警寧澤,帶...
    沈念sama閱讀 35,790評論 5 346
  • 正文 年R本政府宣布弃酌,位于F島的核電站氨菇,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏妓湘。R本人自食惡果不足惜查蓉,卻給世界環(huán)境...
    茶點故事閱讀 41,442評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望榜贴。 院中可真熱鬧豌研,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,996評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至霜浴,卻和暖如春晶衷,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背阴孟。 一陣腳步聲響...
    開封第一講書人閱讀 33,113評論 1 272
  • 我被黑心中介騙來泰國打工晌纫, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人温眉。 一個月前我還...
    沈念sama閱讀 48,332評論 3 373
  • 正文 我出身青樓缸匪,卻偏偏與公主長得像,于是被迫代替她去往敵國和親类溢。 傳聞我的和親對象是個殘疾皇子凌蔬,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,044評論 2 355