利用python代码来实现对京东数据库的操作
from pymysql import connect
def search_all_info():
'''查询所有商品信息'''
#1.从数据库得到数据
#1.1连接数据库
#1.2执行sql语句
#1.3关闭
#2.处理数据
#链接数据库
conn = connect(host='localhost',port=3306,user='root',password='mysql',database='jing_dong',charset='utf8')
#得到游标
cs1 = conn.cursor()
#执行sql语句
sql = '''select * from goods;'''
print(sql)
cs1.execute(sql)
#得到数据
data = cs1.fetchall()
#打印所有数据
for temp in data:
print(temp)
#关闭
cs1.close()
conn.close()
def search_all_cate_info():
'''查询所有的种类信息'''
#1.从数据库得到数据
#1.1连接数据库
#1.2执行sql语句
#1.3关闭
#2.处理数据
#连接数据库
conn = connect(host='localhost',port='3306',user='root',password='mysql',database='jing_dong',charset='utf8')
#得到游标
cs1 = conn.cursor()
#执行sql语句
sql = """select * from goods_cates;"""
cs1.execute(sql)
#得到数据
data = cs1.fetchall()
for temp in data:
print(temp)
#关闭
cs1.close()
conn.close()
def search_all_brand_info():
'''查询所有的品牌信息'''
#连接数据库
conn = connect(host='localhost',port=3306,user='root',password='mysql',database='jing_dong',charset='utf8')
#得到游标
cs1 = conn.cursor()
#执行sql语句
sql = """select * from goods_brands;"""
cs1.execute(sql)
#得到数据
data = cs1.fetchall()
#遍历每一个数据
for temp in data:
print(temp)
#关闭
cs1.close()
conn.close()
def add_cate_info(cate_name):
'''添加商品信息'''
#链接数据库
conn = connect(host='localhost',port=3306,user='root',password='mysql',database='jing_dong',charset='utf8')
#得到游标
cs1 = conn.cursor()
#执行sql语句
sql = """insert into goods_cates(name) values('%s');""" % cate_name
print(sql)
cs1.execute(sql)
#得到数据
data = cs1.fetchall()
for temp in data:
print(temp)
#提交数据
conn.commit()
#关闭
cs1.close()
conn.close()
def search_into_by_id(id):
'''根据id来查询商品的信息'''
#链接数据库
conn = connect(host='localhost',port=3306,user='root',password='mysql',database='jing_dong',charset='utf8')
#得到游标
cs1 = conn.cursor()
#执行sql语句
sql = """select * from goods where id = %s;""" % id
print(id)
cs1.execute(sql)
#得到数据并遍历
data = cs1.fetchall()
for temp in data:
print(temp)
#关闭
cs1.close()
conn.close()
def search_into_by_safe_id(id):
'''以安全的方式根据id来查询商品信息'''
#链接数据库
conn = connect(host='localhost',port=3306,user='root',password='mysql',charset='utf8',database='jing_dong')
#得到游标
cs1 = conn.cursor()
#执行sql语句
sql = """select * from goods where id = %s;"""
print(sql)
cs1.execute(sql,(id,))
#得到数据
data = cs1.fetchall()
for temp in data:
print(temp)
#关闭
cs1.close()
conn.close()
def run_server():
'''京东数据库的操作练习'''
while True:
# 死循环打印操作的信息
print("查询所有商品信息")
print("查询所有商品在种类信息")
print("查询所有商品在品牌信息")
print("添加商品种类")
print("根据id查询商品信息")
print("根据id查询商品信息安全方式")
command = input('请输入要操作的信息:')
# 判断指令
if command == '1':
'''查询所有商品信息'''
search_all_info()
if command == '2':
'''查询所有商品的种类信息'''
search_all_cate_info()
if command == '3':
'''查询所有商品的品牌信息'''
search_all_brand_info()
if command == '4':
'''添加商品种类'''
cate_name = input("请输入要添加的商品种类名称:")
add_cate_info(cate_name)
if command == '5':
'''根据id查询商品信息'''
id = input('请输入要查询的商品的id:')
search_into_by_id(id)
if command == '6':
'''根据id查询商品信息的安全方式'''
id = input('请输入要查询商品信息的id:')
search_into_by_safe_id(id)
else:
print('您输入的编号有误,请重新输入')
def main():
'''开启京东服务器'''
run_server()
if __name__ == '__main__':
main()