"""
笔记
1.pymysql
2.视图(主要是用来查询):
select 语句运行之后的一个结果集 就是一条查询语句
就是一个虚拟表 减少sql语句的复杂性 还能提高可读性 方便查询
定义视图:create view 视图名字 as select 语句
查看视图:show tables;
使用视图:select * from 视图名字
删除视图:drop vlew 视图名字
有时间看看docker容器
作业 把redis数据库列表封装
"""
import pymysql
# 自带事务要自己关闭
# 一定要添加库不然会报错
# 要写异常
db_config = {
"host": "127.0.0.1",
"port": 3306,
"user": "root",
"password": "qwe123",
"db": "xiwen",
"charset": "utf8",
}
# 1. connect:连接 获取链接对象(ip port user password db utf—8)
conn = pymysql.connect(**db_config)
# 2. 游标对像 就像c里面的指针
cur = conn.cursor()
# 查询
sql = "select * from task1"
# 添加 str嵌套需要注意
# sql = "insert into task1 values(9,'fafa',19,1)"
try:
# 执行sql语句
ry = cur.execute(sql)
# 查看一条
# print(cur.fetchone())
# 查看多条 自己指定条数
# print(cur.fetchmany(3))
# 全部显示出来 一般不会这样用,数据太多会崩溃的
print(cur.fetchall())
except Exception as e:
print(e)
# 回滚数据 回滚到没有操作之前
conn.rollback()
# 如果没有出错就提交
else:
conn.commit()
# 必须执行
finally:
# 关闭游标
cur.close()
# 关闭连接
conn.close()
# # 事务的提交
# conn.commit()
#
# # 关闭游标
# cur.close()
#
# # 关闭连接
# conn.close()
# import redis
#
#
# class Redis:
# def __init__(self, db=0):
# self.client = redis.StrictRedis(host="127.0.0.1", decode_responses=True)
# self.db = db
#
# # 增
# def lpush(self, key, *data, lpush=True):
# if lpush:
# self.client.lpush(key, data)
# elif lpush == False:
# self.client.rpush(key, data)
#
# # 查
# def get(self, key, start_index, end_index=None):
# if end_index:
# return self.client.lrange(key, start_index, end_index)
# else:
# return self.client.lindex(key, start_index)
#
# # 改
# def lset(self, key, index, value):
# self.client.lset(key, index, value)
#
# # 删
# def pop(self, key, rpop=True):
# if rpop:
# self.client.rpop(key)
# else:
# self.client.lpop(key)
#
# def lrem(self, key, a, value):
# self.client.lrem(key, a, value)
#
#
# if __name__ == '__main__':
# red = Redis()
# 增
# red.lpush("lname", "1", "2")
# red.lpush("lname", "7", "8", lpush=False)
# 查
# print(red.get("lname", 0))
# print(red.get("lname", 0,-1))
# 改
# red.lset("lname", 1, "cctv")
# 删
# red.pop("lname", rpop=False)
# red.lrem("lname",0,"cctv")
# import pymongo
#
# # 建立连接
# client = pymongo.MongoClient("127.0.0.1", 27017)
# # 指定数据库
# xiwen = client["xiwen"]
# # 指定集合
# class51 = xiwen["class51"]
# 增
# 增加一条
# class51.insert_one({"name":"fafa","age":18})
# 增加多条
# class51.insert_many([{"name":"小明","age":18},{"name":"小红","age":19}])
# 查
# 查一条
# find = class51.find_one({"name": "小明"})
# 查多条
# find = class51.find()
# for i in find:
# print(i)
# 改
# 修改一条
# update = class51.update_one({"name": "小明"}, {"$set": {"name": "小王"}})
# 修改多条
# update = class51.update_many({"age": 18}, {"$set": {"name": "小李"}})
# 删
# 删一条
# class51.delete_one({"name": "小明"})
# 删除多条或全部
# class51.delete_many({"name": "小红"})
# find = class51.find()
# for i in find:
# print(i)
import pymongo
class MongoDB:
def __init__(self, database, collection):
# 建立连接
self.client = pymongo.MongoClient("127.0.0.1", 27017)
# 指定数据库库
self.xiwen = self.client[database]
# 指定集合
self.col = self.xiwen[collection]
# 增
def insert(self, data, onlyone=True):
if onlyone:
# 字典
self.col.insert_one(data)
elif onlyone == False:
# 列表
self.col.insert_many(data)
# 查
def find(self, onlyone=True):
if onlyone:
find = self.col.find_one()
print(find)
elif onlyone == False:
find = self.col.find()
for i in find:
print(i)
# 改
def update(self, data, new_data, onlyone=True):
if onlyone:
self.col.update_one(data, {"$set": new_data})
elif onlyone == False:
self.col.update_many(data, {"$set": new_data})
# 删
def delete(self, data, onlyone=True):
if onlyone and data != {}:
self.col.delete_one(data)
else:
self.col.delete_many(data)
if __name__ == '__main__':
MG = MongoDB("xiwen", "students")
# MG.insert({"name": "希文"}) # 添加一条数据
# MG.insert([{"name": "小明", "age": 18}, {"name": "小红", "age": 19}],False) # 添加多条数据
# MG.update({"name": "小红"}, {"name": "小李"})
# MG.update({"name": "希文"}, {"name": "小明"}, False)
MG.delete({"name": "小明"}, False)
MG.find(False)
mysql,redis,monggodb,python交互
猜你喜欢
转载自blog.csdn.net/weixin_44961387/article/details/100566642
今日推荐
周排行