sephore增刪改查衡未,添加分類,SQL注入

1家凯、引入模塊

在py文件中引入pymysql模塊

from pymysql import connect

2缓醋、Connection 對(duì)象

用于建立與數(shù)據(jù)庫的連接

創(chuàng)建對(duì)象:調(diào)用connect()方法

conn=connect(參數(shù)列表)

參數(shù)host:連接的mysql主機(jī),如果本機(jī)是'localhost'

參數(shù)port:連接的mysql主機(jī)的端口绊诲,默認(rèn)是3306

參數(shù)database:數(shù)據(jù)庫的名稱sephore

參數(shù)user:連接的用戶名root

參數(shù)password:連接的密碼


對(duì)象的方法

close()關(guān)閉連接

commit()提交

cursor()返回Cursor對(duì)象送粱,用于執(zhí)行sql語句并獲得結(jié)果

3、Cursor對(duì)象

用于執(zhí)行sql語句掂之,使用頻度最高的語句為select抗俄、insert、update世舰、delete

獲取Cursor對(duì)象:調(diào)用Connection對(duì)象的cursor()方法

cs1=conn.cursor()

對(duì)象的方法

1.????close()關(guān)閉

2.????execute(operation [, parameters ])執(zhí)行語句动雹,返回受影響的行數(shù),主要用于執(zhí)行insert跟压、update胰蝠、delete語句也可以執(zhí)行create、alter茸塞、drop等語句

3.????fetchone()執(zhí)行查詢語句時(shí)躲庄,獲取查詢結(jié)果集的第一個(gè)行數(shù)據(jù),返回一個(gè)元組

4.????fetchall()執(zhí)行查詢時(shí)翔横,獲取結(jié)果集的所有行读跷,一行構(gòu)成一個(gè)元組,再將這些元組裝入一個(gè)元組返回

對(duì)象的屬性

rowcount只讀屬性禾唁,表示最近一次execute()執(zhí)行后受影響的行數(shù)

connection獲得當(dāng)前連接對(duì)象

數(shù)據(jù)查詢

from pymysql import connect

class SEPHORE(object):

def __init__(self):

# 創(chuàng)建Connection連接

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

# 獲得Cursor對(duì)象

self.cursor = self.conn.cursor()

def __del__(self):

# 關(guān)閉Cursor對(duì)象

self.cursor.close()

self.conn.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 * 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)

@staticmethod

def ptint_menu():

print('-----絲芙蘭-----')

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

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

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

return input('請(qǐng)輸入功能對(duì)應(yīng)的序號(hào):')

def run(self):

while True:

num = self.ptint_menu()

if num == '1':

# 查詢所有商品

self.show_all_items()

elif num == '2':

# 查詢分類

self.show_cates()

elif num == '3':

# 查詢品牌分類

self.show_brands()

else:

print('輸入有誤效览,請(qǐng)重新輸入....')

def main():

#1,創(chuàng)建一個(gè)絲芙蘭商城對(duì)象

sephore = SEPHORE()

# 2,調(diào)用這個(gè)對(duì)象run 方法,讓其運(yùn)行

sephore.run()

if __name__ == '__main__':

main()

添加一個(gè)商品分類

from pymysql import connect

class SEPHORE(object):

def __init__(self):

# 創(chuàng)建Connection連接

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

# 獲得Cursor對(duì)象

self.cursor = self.conn.cursor()

def __del__(self):

# 關(guān)閉Cursor對(duì)象

self.cursor.close()

self.conn.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 * from goods;"

self.execute_sql(sql)

def show_cates(self):

sql = "select * from goods_cates;"

self.execute_sql(sql)

def show_brands(self):

sql = "select * from goods_brands;"

self.execute_sql(sql)

def add_cates(self):

item_name = input("請(qǐng)輸入新商品分類的名稱")

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

self.cursor.execute(sql)

self.conn.commit()

@staticmethod

def ptint_menu():

print('-----絲芙蘭-----')

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

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

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

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

return input('請(qǐng)輸入功能對(duì)應(yīng)的序號(hào):')

def run(self):

while True:

num = self.ptint_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()

else:

print('輸入有誤荡短,請(qǐng)重新輸入....')

def main():

#1,創(chuàng)建一個(gè)絲芙蘭商城對(duì)象

sephore = SEPHORE()

# 2,調(diào)用這個(gè)對(duì)象run 方法丐枉,讓其運(yùn)行

sephore.run()

if __name__ == '__main__':

main()

SQL注入(sephore增刪改查)

from pymysql import connect

class SEPHORE(object):

def __init__(self):

self.is_login = False

# 創(chuàng)建Connection連接

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

# 獲得Cursor對(duì)象

self.cursor = self.conn.cursor()

def __del__(self):

# 關(guān)閉Cursor對(duì)象

self.cursor.close()

self.conn.close()

def execute_sql(self,sql):

self.cursor.execute(sql)

for temp in self.cursor.fetchall():

print(temp)

def add_customers(self):

# item_id = input('請(qǐng)輸入id')

item_name = input('請(qǐng)輸入姓名')

item_tel = input("請(qǐng)輸入手機(jī)號(hào)")

item_passwd = input('請(qǐng)輸入密碼')

item_address = input('請(qǐng)輸入收貨地址')

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

# sql = "insert into customers where id=%d,name=%s,tel=%d,passwd=%d,address=%s"

self.cursor.execute(sql,[item_name,item_tel,item_passwd,item_address])

self.conn.commit()

print("注冊(cè)成功了")

def login_customers(self):

login_name = input('請(qǐng)輸入name')

login_passwd = input('請(qǐng)輸入密碼')

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

self.cursor.execute(sql,[login_name,login_passwd])

self.uid = self.cursor.fetchone()

if self.uid:

self.uid = self.uid[0]

print(self.uid)

self.is_login = True

return self.is_login

# print(self.cursor.fetchall())

def show_all_items(self):

"""顯示所有商品"""

sql = "select * from goods;"

self.execute_sql(sql)

def show_cates(self):

sql = "select * from goods_cates;"

self.execute_sql(sql)

def show_brands(self):

sql = "select * from goods_brands;"

self.execute_sql(sql)

def add_cates(self):

item_name = input("請(qǐng)輸入新商品分類的名稱")

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

self.cursor.execute(sql)

self.conn.commit()

def? delete_cates(self):

id = 0

while True:

try:

id = int(input("請(qǐng)輸入要?jiǎng)h除的商品分類的id"))

except Exception as ret:

print("請(qǐng)輸入正確的id編號(hào)")

else:

break

isdel = input("您確認(rèn)要?jiǎng)h除編號(hào)為%s的商品分類?按y確認(rèn)掘托,其他鍵返回主菜單")

if isdel == 'y':

sql = "update goods_cates set is_show=0 where id=%s;"

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

self.conn.commit()

pass

def update_cates(self):

id = 0

while True:

try:

id = int(input("請(qǐng)輸入要修改的商品分類的id:"))

except Exception as ret:

print("請(qǐng)輸入正確的id編號(hào)")

else:

break

item_name = input("請(qǐng)輸入要新的商品分類的名稱")

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

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

self.conn.commit()

def get_info_by_name(self):

find_name = input('請(qǐng)輸入要查詢的商品的名字')

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

self.cursor.execute(sql,[find_name]) #防止SQL注入

print(self.cursor.fetchall())

def create_order(self):

pid = input("請(qǐng)輸入商品的編號(hào)")

num = input("請(qǐng)輸入商品的數(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])

# print("cursor.lastrowid-->",self.cursor.lastrowid)

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("您要購(gòu)買的商品已下架瘦锹!")

@staticmethod

def ptint_customers():

print('-----絲芙蘭-----')

print('1:注冊(cè)')

print('2:登錄')

print("3:退出")

return input('請(qǐng)輸入功能對(duì)應(yīng)的序號(hào):')

@staticmethod

def ptint_menu():

print('-----絲芙蘭-----')

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

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

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

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

print("5:刪除商品分類")

print("6:修改商品分類")

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

print("8下單")

return input('請(qǐng)輸入功能對(duì)應(yīng)的序號(hào):')

def run(self):

while True:

num = self.ptint_customers()

if num == '1':

self.add_customers()

elif num == '2':

if self.login_customers():

print("登錄成功")

break

else:

print("用戶名或密碼有誤,請(qǐng)重新輸入")

elif num == "3":

breakx

else:

print('輸入有誤闪盔,請(qǐng)重新輸入....')

# def run(self):

if self.is_login:

while True:

num = self.ptint_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.delete_cates()

elif num == '6':

# 修改商品分類

self.update_cates()

elif num == '7':

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

self.get_info_by_name()

else:

print('輸入有誤弯院,請(qǐng)重新輸入....')

def main():

#1,創(chuàng)建一個(gè)絲芙蘭商城對(duì)象

sephore = SEPHORE()

# 2,調(diào)用這個(gè)對(duì)象run 方法,讓其運(yùn)行

sephore.run()

if __name__ == '__main__':

main()

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末泪掀,一起剝皮案震驚了整個(gè)濱河市听绳,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌异赫,老刑警劉巖椅挣,帶你破解...
    沈念sama閱讀 218,607評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異塔拳,居然都是意外死亡鼠证,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,239評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門靠抑,熙熙樓的掌柜王于貴愁眉苦臉地迎上來量九,“玉大人,你說我怎么就攤上這事颂碧≤校” “怎么了?”我有些...
    開封第一講書人閱讀 164,960評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵稚伍,是天一觀的道長(zhǎng)弯予。 經(jīng)常有香客問我戚宦,道長(zhǎng)个曙,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,750評(píng)論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮垦搬,結(jié)果婚禮上呼寸,老公的妹妹穿的比我還像新娘。我一直安慰自己猴贰,他們只是感情好对雪,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,764評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著米绕,像睡著了一般瑟捣。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上栅干,一...
    開封第一講書人閱讀 51,604評(píng)論 1 305
  • 那天迈套,我揣著相機(jī)與錄音,去河邊找鬼碱鳞。 笑死桑李,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的窿给。 我是一名探鬼主播贵白,決...
    沈念sama閱讀 40,347評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼崩泡!你這毒婦竟也來了禁荒?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,253評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤允华,失蹤者是張志新(化名)和其女友劉穎圈浇,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體靴寂,經(jīng)...
    沈念sama閱讀 45,702評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡磷蜀,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,893評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了百炬。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片褐隆。...
    茶點(diǎn)故事閱讀 40,015評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖剖踊,靈堂內(nèi)的尸體忽然破棺而出庶弃,到底是詐尸還是另有隱情,我是刑警寧澤德澈,帶...
    沈念sama閱讀 35,734評(píng)論 5 346
  • 正文 年R本政府宣布歇攻,位于F島的核電站,受9級(jí)特大地震影響梆造,放射性物質(zhì)發(fā)生泄漏缴守。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,352評(píng)論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望屡穗。 院中可真熱鬧贴捡,春花似錦、人聲如沸村砂。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,934評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽础废。三九已至汛骂,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間评腺,已是汗流浹背香缺。 一陣腳步聲響...
    開封第一講書人閱讀 33,052評(píng)論 1 270
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留歇僧,地道東北人图张。 一個(gè)月前我還...
    沈念sama閱讀 48,216評(píng)論 3 371
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像诈悍,于是被迫代替她去往敵國(guó)和親祸轮。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,969評(píng)論 2 355

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