# pcj from MySQLdb import * class JD(object): def __init__(self): '''连接数据库''' self.conn=connect(host='localhost',port=3306,user='root',password='123456',database='jd',charset='utf8') #获取游标 self.cur=self.conn.cursor() def __del__(self): self.cur.close() self.conn.close() def excute_sql(self,sql): self.cur.execute(sql) for i in self.cur.fetchall(): print(i) def show_goods(self): sql='select * from goods;' self.excute_sql(sql) def show_cates(self): sql='select * from goods_cates;' self.excute_sql(sql) def add_cates(self): name=input('请输入新类别:') sql='''insert into goods_cates (name) values ('%s')'''% name self.cur.execute(sql) self.conn.commit() def get_goods_name(self): find_name=input('请输入高品名称') sql='select * from goods where name=%s' self.cur.execute(sql,[find_name]) print(self.cur.fetchall()) def run(self): while True: print('----商品查询-----') print('1、查询所有商品') print('2、查询所有分类') print('3、添加一个分类') print('4、查询一个商品') op = input('请输入功能号') if op=="1": self.show_goods() elif op=='2': self.show_cates() elif op=='3': self.add_cates() elif op=='4': self.get_goods_name() def main(): #连接数据库 jd=JD() #运行 jd.run() if __name__ == '__main__': main()