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注入(sephore增刪改查)
from pymysql import connect
class SEPHORE(object):
def __init__(self):
self.is_login = False
# 創建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 add_customers(self):
# item_id = input('請輸入id')
item_name = input('請輸入姓名')
item_tel = input("請輸入手機號")
item_passwd = input('請輸入密碼')
item_address = input('請輸入收貨地址')
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("注冊成功了")
def login_customers(self):
login_name = input('請輸入name')
login_passwd = input('請輸入密碼')
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("請輸入新商品分類的名稱")
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("請輸入要刪除的商品分類的id"))
except Exception as ret:
print("請輸入正確的id編號")
else:
break
isdel = input("您確認要刪除編號為%s的商品分類?按y確認,其他鍵返回主菜單")
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("請輸入要修改的商品分類的id:"))
except Exception as ret:
print("請輸入正確的id編號")
else:
break
item_name = input("請輸入要新的商品分類的名稱")
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('請輸入要查詢的商品的名字')
sql = "select * from goods where name=%s"
self.cursor.execute(sql,[find_name]) #防止SQL注入
print(self.cursor.fetchall())
def create_order(self):
pid = input("請輸入商品的編號")
num = input("請輸入商品的數量")
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("您要購買的商品已下架!")
@staticmethod
def ptint_customers():
print('-----絲芙蘭-----')
print('1:注冊')
print('2:登錄')
print("3:退出")
return input('請輸入功能對應的序號:')
@staticmethod
def ptint_menu():
print('-----絲芙蘭-----')
print("1:所有的商品")
print("2:所有的商品分類")
print("3:所有的商品品牌分類")
print("4:添加商品分類")
print("5:刪除商品分類")
print("6:修改商品分類")
print("7:根據名字查詢商品")
print("8下單")
return input('請輸入功能對應的序號:')
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("用戶名或密碼有誤,請重新輸入")
elif num == "3":
breakx
else:
print('輸入有誤,請重新輸入....')
# 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':
# 根據名字查詢商品
self.get_info_by_name()
else:
print('輸入有誤,請重新輸入....')
def main():
#1,創建一個絲芙蘭商城對象
sephore = SEPHORE()
# 2,調用這個對象run 方法,讓其運行
sephore.run()
if __name__ == '__main__':
main()