from pymysql import *
def main():
conn = connect(host = "192.168.3.55", port = 3306, user = 'root', password = '', database = 'jing_dong', charset = 'utf8')
cs1 = conn.cursor()
count = cs1.execute('select id, name from goods where id > 4')
print("打印受影响的行数:", count)
for i in range(count):
result = cs1.fetchone()
print(result)
cs1.close()
conn.close()
if __name__ == '__main__':
main()
from pymysql import *
class JD(object):
def __init__(self):
self.conn = connect(host="localhost", port=3306, user='root', password='qwe123123', database='jingdong', charset='utf8')
self.cursor = self.conn.cursor()
def __del__(self):
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_brand;"
self.execute_sql(sql)
@staticmethod
def print_menu():
print("--------------------京东--------------------")
print("1:所有的商品")
print("2:所有的商品分类")
print("3:所有的商品品牌分类")
num = input("请输入对应功能序号:")
return num
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()
else:
print("输入有误,请重新输入!")
def main():
jd = JD()
jd.run()
if __name__ == "__main__":
main()
from pymysql import *
class JD(object):
def __init__(self):
self.conn = connect(host="localhost", port=3306, user='root', password='qwe123123', database='jingdong', charset='utf8')
self.cursor = self.conn.cursor()
def __del__(self):
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_brand;"
self.execute_sql(sql)
def add_brands(self):
item_name = input("输入新商品名称:")
sql = "insert into goods_brand (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:添加商品品牌")
num = input("请输入对应功能序号:")
return num
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_brands()
else:
print("输入有误,请重新输入!")
def main():
jd = JD()
jd.run()
if __name__ == "__main__":
main()