查詢數(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確認谐算,其他鍵返回主菜單:'%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閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件艰毒,死亡現(xiàn)場離奇詭異筐高,居然都是意外死亡,警方通過查閱死者的電腦和手機丑瞧,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進店門柑土,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人绊汹,你說我怎么就攤上這事冰单。” “怎么了灸促?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵诫欠,是天一觀的道長涵卵。 經(jīng)常有香客問我,道長荒叼,這世上最難降的妖魔是什么轿偎? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮被廓,結(jié)果婚禮上坏晦,老公的妹妹穿的比我還像新娘。我一直安慰自己嫁乘,他們只是感情好昆婿,可當我...
    茶點故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著蜓斧,像睡著了一般仓蛆。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上挎春,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天看疙,我揣著相機與錄音,去河邊找鬼直奋。 笑死能庆,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的脚线。 我是一名探鬼主播搁胆,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼邮绿!你這毒婦竟也來了渠旁?” 一聲冷哼從身側(cè)響起荤胁,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤尤溜,失蹤者是張志新(化名)和其女友劉穎诉濒,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體傻唾,經(jīng)...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年承耿,在試婚紗的時候發(fā)現(xiàn)自己被綠了冠骄。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡加袋,死狀恐怖凛辣,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情职烧,我是刑警寧澤扁誓,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布防泵,位于F島的核電站,受9級特大地震影響蝗敢,放射性物質(zhì)發(fā)生泄漏捷泞。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一寿谴、第九天 我趴在偏房一處隱蔽的房頂上張望锁右。 院中可真熱鬧,春花似錦讶泰、人聲如沸咏瑟。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽码泞。三九已至,卻和暖如春惠桃,著一層夾襖步出監(jiān)牢的瞬間浦夷,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工辜王, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留劈狐,地道東北人。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓呐馆,卻偏偏與公主長得像肥缔,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子汹来,可洞房花燭夜當晚...
    茶點故事閱讀 44,979評論 2 355

推薦閱讀更多精彩內(nèi)容