from pymysql import connect
class JD(object):
def __init__(self):
self.connect = connect(host='localhost',port=3306,user='root',password='mysql',database='jing_dong',charset='utf8')
self.cursor=self.connect.cursor()
def execute(self,sql):
self.cursor.execute(sql)
for i in self.cursor.fetchall():
print(i)
def all_goods(self):
sql="select * from goods;"
self.execute(sql)
def all_price(self):
sql="select name,price from goods;"
self.execute(sql)
def all_brands(self):
sql="select * from goods_brands;"
self.execute(sql)
def insert(self):
str = input("商品名称:")
sql = "insert into goods_brands (name) values (%s);"
self.cursor.execute(sql,[str])
# self.cursor.execute("insert into goods_brands (name) values ('%s');"%str)
self.connect.commit()
def __del__(self):
self.cursor.close()
self.connect.close()
def print_(self):
print("查询的所有商品请按【1】、品牌请按【2】、价格请按【3】、添加商品【4】: ")
a=input("请输入查询编码:")
return a
def run(self):
while True:
num = self.print_()
if num=="1":
self.all_goods()
elif num=="2":
self.all_brands()
elif num=="3":
self.all_price()
elif num=="4":
self.insert()
else:
break
def main():
jd = JD()
jd.run()
if __name__ == '__main__':
main()