import pymysql
'''
conn.commit() 真正将数据写入数据库
conn.rollback() 取消前面是sql语句操作
'''
class JD():
def __init__(self):
self.conn = pymysql.connect('localhost','root','','python_test')
self.cursor = self.conn.cursor()
# cursor.close()
# conn.close()
# cursor.execute('select * from tdb_goods')
def sql_exe(self,sql):
self.cursor.execute(sql)
ret = self.cursor.fetchall()
return ret
def show_all_item(self):
for temp in self.sql_exe('select * from tdb_goods'):
print(temp)
def show_goods_cate(self):
for temp in self.sql_exe('select * from goods_cate'):
print(temp)
def show_brand_name(self):
for temp in self.sql_exe('select * from brand_name'):
print(temp)
def add_brand_name(self):
brandname = input('请输入你要添加的品牌名称:')
sql = """insert into brand_name (name) values ("%s")""" % brandname
self.cursor.execute(sql)
self.conn.commit()
def get_info_goods(self):
brandname = input('请输入你查找的的商品名称:')
sql = 'select * from brand_name where name=%s'
self.cursor.execute(sql, [brandname])
print(self.cursor.fetchall())
@staticmethod
def mue():
print("......京东商城......")
print('1:所有的商品')
print('2:所有商品的分类')
print('3:所有的商品品牌分类')
print('4:添加商品品牌')
print('5:搜索商品详情')
print('0:关闭商城')
return input('请输入功能相对于的序号:')
def run(self):
while True:
num = self.mue()
if num == '1':
self.show_all_item()
elif num == '2':
self.show_goods_cate()
elif num == '3':
self.show_brand_name()
elif num == '0':
break
elif num == '4':
self.add_brand_name()
elif num == '5':
self.get_info_goods()
else:
print('输入有误,请重新输入....')
self.cursor.close()
self.conn.close()
def main():
jd = JD()
jd.run()
if __name__ == '__main__':
main()