MySQL(查詢數(shù)據(jù)、增刪改、添加分類、SQL注入)

查詢數(shù)據(jù)和添加分類

from pymysql import connect

class TSGF(object):

def __init__(self):

# 創(chuàng)建Connection連接

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

# 獲得Cursor對象

self.cursor = self.conn.cursor()

def __del__(self):

#關(guān)閉Cursor對象

cursor.close()

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 * form goods;"

self.execute_sql(sql)

def show_cates(self):

sql = "select name form goods_cates;"

self.execute_sql(sql)

def show_brands(self):

sql = "select name form 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 print_menu():

print('------天使工房------')

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

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

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

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

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

def run(self):

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

else:

print('請重新輸入----')

def main():

#1,創(chuàng)建一個天使工房商城對象

tsgf = TSGF()

#2,調(diào)用這個對象run方法。讓其運行

tsgf.run()

if __name__=='__main__':

main()

增刪改

from pymysql import connect

# 創(chuàng)建Connection連接

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

# 獲得Cursor對象

cursor = conn.cursor()

#print(cursor.execute("""insert into goods_cates (name) values ("硬盤")"""))

#print(cursor.execute("""insert into goods_cates (name) values ("硬盤2")"""))

#print(cursor.execute("""insert into goods_cates (name) values ("硬盤3")"""))

#conn.commit()

print(cursor.execute("""insert into goods_cates (name) values ("硬盤3")"""))

print(cursor.execute("""insert into goods_cates (name) values ("硬盤4")"""))

conn.rollback()#如果反悔時用

print(cursor.execute("""insert into goods_cates (name) values ("硬盤4")"""))

conn.commit()

SQL注入

from pymysql import connect

class TSGF(object):

def __init__(self):

# 創(chuàng)建Connection連接

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

# 獲得Cursor對象

self.cursor = self.conn.cursor()

def __del__(self):

#關(guān)閉Cursor對象

cursor.close()

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 * form goods;"

self.execute_sql(sql)

def show_cates(self):

sql = "select name form goods_cates;"

self.execute_sql(sql)

def show_brands(self):

sql = "select name form 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 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'#防止SQL注入

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

print(self.cursor.fetchall())

@staticmethod

def print_menu():

print('------天使工房------')

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

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

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

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

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

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

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

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

def run(self):

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

#刪除商品分類

elif num == '6':

#修改商品分類

elif num == '7':

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

else:

print('請重新輸入----')

def main():

#1,創(chuàng)建一個天使工房商城對象

tsgf = TSGF()

#2,調(diào)用這個對象run方法。讓其運行

tsgf.run()

if __name__=='__main__':

main()

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容