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

1、引入模塊

在py文件中引入pymysql模塊

from pymysql import connect

2、Connection 對象

用于建立與數據庫的連接

創建對象:調用connect()方法

conn=connect(參數列表)

參數host:連接的mysql主機,如果本機是'localhost'

參數port:連接的mysql主機的端口,默認是3306

參數database:數據庫的名稱sephore

參數user:連接的用戶名root

參數password:連接的密碼

參數charset:通信采用的編碼方式,一般使用utf8

對象的方法

close()關閉連接

commit()提交

cursor()返回Cursor對象,用于執行sql語句并獲得結果

3、Cursor對象

用于執行sql語句,使用頻度最高的語句為select、insert、update、delete

獲取Cursor對象:調用Connection對象的cursor()方法

cs1=conn.cursor()

對象的方法

1.????close()關閉

2.????execute(operation [, parameters ])執行語句,返回受影響的行數,主要用于執行insert、update、delete語句也可以執行create、alter、drop等語句

3.????fetchone()執行查詢語句時,獲取查詢結果集的第一個行數據,返回一個元組

4.????fetchall()執行查詢時,獲取結果集的所有行,一行構成一個元組,再將這些元組裝入一個元組返回

對象的屬性

rowcount只讀屬性,表示最近一次execute()執行后受影響的行數

connection獲得當前連接對象

數據查詢

from pymysql import connect

class SEPHORE(object):

def __init__(self):

# 創建Connection連接

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

# 獲得Cursor對象

self.cursor = self.conn.cursor()

def __del__(self):

# 關閉Cursor對象

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('請輸入功能對應的序號:')

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('輸入有誤,請重新輸入....')

def main():

#1,創建一個絲芙蘭商城對象

sephore = SEPHORE()

# 2,調用這個對象run 方法,讓其運行

sephore.run()

if __name__ == '__main__':

main()

添加一個商品分類

from pymysql import connect

class SEPHORE(object):

def __init__(self):

# 創建Connection連接

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

# 獲得Cursor對象

self.cursor = self.conn.cursor()

def __del__(self):

# 關閉Cursor對象

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("請輸入新商品分類的名稱")

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('請輸入功能對應的序號:')

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('輸入有誤,請重新輸入....')

def main():

#1,創建一個絲芙蘭商城對象

sephore = SEPHORE()

# 2,調用這個對象run 方法,讓其運行

sephore.run()

if __name__ == '__main__':

main()

SQL注入

from pymysql import connect

class SEPHORE(object):

def __init__(self):

# 創建Connection連接

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

# 獲得Cursor對象

self.cursor = self.conn.cursor()

def __del__(self):

# 關閉Cursor對象

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("請輸入新商品分類的名稱")

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

self.cursor.execute(sql)

self.conn.commit()

def? delete_cates(self):

item_name = input("請輸入要刪除的商品分類的名稱")

sql = """delect name from goods_cates (name) values("%s");"""% item_name

self.cursor.execute(sql)

self.conn.commit()

def alter_cates(self):

item_name = input("請輸入要修改的商品分類的名稱")

sql = """alter name from goods_cates (name) values("%s");"""% item_name

self.cursor.execute(sql)

self.conn.commit()

def get_info_by_name(self):

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

# sql = """insert into goods where name= '%s';"""% find_name

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

# self.execute_sql(sql)

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

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

print(self.cursor.fetchall())

@staticmethod

def ptint_menu():

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

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

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

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

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

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

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

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

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

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()

elif num == '5':

# 刪除商品分類

self.delete_cates()

elif num == '6':

# 修改商品分類

self.alter_cates()

elif num == '7':

# 根據名字查詢商品

self.get_info_by_name()

else:

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

def main():

#1,創建一個絲芙蘭商城對象

sephore = SEPHORE()

# 2,調用這個對象run 方法,讓其運行

sephore.run()

if __name__ == '__main__':

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容