一、封装类练习
from pymysql import *
class Mydb():
# 初始化
def __init__(self):
conn = self.conn()
#连接数据库
def conn(self):
try:
self.conn = connect(
host='127.0.0.1',
port=3306,
user='root',
passwd='root',
db='demo1125',
charset='utf8'
)
print('connect successful')
except Exception as e:
print(e)
def get_one(self):
sql = 'select * from cities;'
# 获取游标
cursor = self.conn.cursor()
# 执行sql语句
cursor.execute(sql)
# 获取执行结果
result = cursor.fetchone()
print(result)
cursor.close()
# self.conn.close()
return result
def get_more(self):
sql = 'select * from cities;'
# 获取游标
cursor = self.conn.cursor()
# 执行sql语句
cursor.execute(sql)
# 获取执行结果
result = cursor.fetchall()
# print(result)
cursor.close()
# self.conn.close()
return result
# 关闭连接
# def close_conn(self):
# self.conn.close()
# 销毁对象的时候顺便关闭连接
def __del__(self):
# cursor.close()
self.conn.close()
def main():
obj = Mydb()
res = obj.get_more()
# obj.get_one()
for item in res:
print(item)
if __name__ == '__main__':
main()
结果:
connect successful
(1, ‘110100’, ‘北京市’, ‘110000’)
(2, ‘1102xx’, ‘北京下属县’, ‘1100xx’)
(3, ‘120100’, ‘天津市’, ‘120000’)
(4, ‘1202xx’, ‘天津下属县’, ‘1200xx’)
(5, ‘130100’, ‘石家庄市’, ‘130000’)
(6, ‘130200’, ‘唐山市’, ‘130000’)
二、商品练习查询
from pymysql import *
class Jd(object):
def __init__(self):
# 连接数据库
self.conn = connect(
host='127.0.0.1',
port=3306,
user='root',
passwd='root',
db='jd',
charset='utf8'
)
# 获取游标
self.cursor = self.conn.cursor()
# @staticmethod 静态方法不需要self这个参数
@staticmethod
def print_menu():
print('---Jd Shop')
print('1-查询所有的商品')
print('2-查询所有的种类')
print('3-查询所有的品牌')
print('4-退出')
num = input('请输入功能数字:')
return num
# 在原来的基础上抽离相同的代码变成一个方法
def execute_sql(self,sql):
# 执行sql
self.cursor.execute(sql)
# 获取结果
result = self.cursor.fetchall()
for item in result:
print(item)
# 1-查询所有的商品的方法
def show_all_goods(self):
sql = 'select * from goods;'
# self.cursor.execute(sql)
# res = self.cursor.fetchall()
# for item in res:
# print(item)
self.execute_sql(sql)
# 2 - 查询所有的种类的方法
def show_all_cate(self):
sql = 'select * from goods_cates;'
self.execute_sql(sql)
# 3 - 查询所有的品牌的方法
def show_all_brand(self):
sql = 'select distinct brand_name from goods;'
self.execute_sql(sql)
def run(self):
while True:
num = self.print_menu()
if num == '1':
self.show_all_goods()
elif num == '2':
self.show_all_cate()
elif num == '3':
self.show_all_brand()
elif num == '4':
break
else:
print('Please input again')
def main():
jd = Jd()
jd.run()
if __name__ == '__main__':
main()
结果:
---Jd Shop
1-查询所有的商品
2-查询所有的种类
3-查询所有的品牌
4-退出
请输入功能数字:1
(1, 'r510vc 15.6英寸笔记本', '5', '华硕', Decimal('3399.000'), 1, 0)
(2, 'y400n 14.0英寸笔记本电脑', '5', '联想', Decimal('4999.000'), 1, 0)
---Jd Shop
1-查询所有的商品
2-查询所有的种类
3-查询所有的品牌
4-退出
请输入功能数字:2
(1, '台式机')
(2, '平板电脑')
---Jd Shop
1-查询所有的商品
2-查询所有的种类
3-查询所有的品牌
4-退出
请输入功能数字:3
('华硕',)
('联想',)
---Jd Shop
1-查询所有的商品
2-查询所有的种类
3-查询所有的品牌
4-退出
请输入功能数字:4
Process finished with exit code 0
三、数据库的增删改
from pymysql import *
def option_db():
try:
conn = connect(host='127.0.0.1',port=3306, user='root',passwd='root', db='jd',charset='utf8')
cursor = conn.cursor()
sql = "insert into goods(`name`) value('MAC5');"
cursor.execute(sql)
sql = "insert into goods(`name`) value('ip5');"
cursor.execute(sql)
# 提交事务 id=15 id=16 id=17 没有提交到
conn.commit()
cursor.close()
conn.close()
except Exception as e:
print('error')
# 部分提交
# conn.commit()
# 回滚 ---银行转账用到
# conn.rollback()
if __name__ == '__main__':
option_db()