pymongo

  1. from pymongo import *
  2. class TestMongo(object):
  3. '''
  4. 初始化函数
  5. '''
  6. def __init__(self,ip,port):
  7. self.clinet = MongoClient(ip, port)
  8. self.db=self.clinet["mytest"].plan
  9. pass
  10. def add_one(self):
  11. import time
  12. post={
  13. "title":'标题',
  14. "content":'内容描述',
  15. "creatTime":time.time()
  16. }
  17. res=self.db.insert_one(post)
  18. return res
  19. pass
  20. def select_data(self,where):
  21. '''
  22. 查询数据
  23. :param where:
  24. :return:
  25. '''
  26. return self.db.find(where)
  27. pass
  28. def update(self,where,content):
  29. self.db.update(where,{"$set":content})
  30. # print(__name__)
  31. if __name__=="__main__":
  32. obj=TestMongo('192.168.136.134',27017)
  33. obj.add_one()
  34. print('添加成功....')
  35. rs=obj.select_data({})
  36. for item in rs:
  37. print(item)
  38. # 建立连接对象
  39. # try:
  40. # clinet=MongoClient('192.168.136.134',27017)
  41. # #创建数据库
  42. # db=clinet.mytest
  43. # colTable=db.plan
  44. # # 插入一条数据
  45. # # colTable.insert_one({'name':'狄仁杰','age':18,'gender':1})
  46. # # 插入多条数据
  47. # # colTable.insert_many([
  48. # # {'name': '狄仁杰1', 'age': 18, 'gender': 1},
  49. # # {'name': '狄仁杰2', 'age': 18, 'gender': 1},
  50. # # {'name': '狄仁杰3', 'age': 18, 'gender': 1},
  51. # # {'name': '狄仁杰4', 'age': 18, 'gender': 1},
  52. # # {'name': '狄仁杰5', 'age': 18, 'gender': 1},
  53. # # {'name': '狄仁杰6', 'age': 18, 'gender': 1},
  54. # # ])
  55. # # 更新一条
  56. # # colTable.update_one({'name': '狄仁杰1'},{"$set":{'age':20}})
  57. # # //更新多条
  58. # # colTable.update_many({'gender': 1}, {"$set": {'age': 20}})
  59. # # 删除多条
  60. # # colTable.delete_many({'gender': 1})
  61. # # 查询的使用
  62. # # res=colTable.find({"name":"狄仁杰1"})
  63. # res = colTable.find_one({"name": "狄仁杰1"})
  64. # print(res)
  65. # # for item in res:
  66. # # print(item)
  67. # # print('sucess')
  68. # except Exception as ex:
  69. # print(ex)

pymysql

  1. # 安装一个mysql中间模块 pip install pymysql
  2. from pymysql import *
  3. class TestMmysql(object):
  4. '''
  5. 初始化函数
  6. '''
  7. def __init__(self,address,user,pwd,database):
  8. res = address.split(":")
  9. self.clinet = conn = connect(host=res[0], user=user, password=pwd, database=database, port=res[1], charset='utf8')
  10. self.sql=conn.cursor() #创建游标
  11. pass
  12. def add_one(self):
  13. import time
  14. post="insert into tb_student(title,content,creaTime) values('标题','内容描述',{})".format(time.time())
  15. self.sql.execute(post)
  16. self.sql.commit()
  17. return "success"
  18. pass
  19. def select_data(self,where):
  20. '''
  21. 查询数据
  22. :param where:
  23. :return:
  24. '''
  25. res=sql.execute('select * from {}'.format(where))
  26. sql.fetchall()
  27. return res
  28. pass
  29. def update(self,where,content):
  30. self.sql.execute('update tb_student set content={} where creaTime={}'.format(content,where))
  31. self.sql.commit()
  32. # print(__name__)
  33. if __name__=="__main__":
  34. obj=TestMmysql('192.168.136.134',3389)
  35. try:
  36. obj.add_one()
  37. print('添加成功....')
  38. rs=obj.select_data('tb_student')
  39. for item in rs:
  40. print(item)
  41. except Exception as ex:
  42. print(ex)
  43. finally:
  44. sql.close()
  45. conn.close()

redis

  1. # 安装一个redis中间模块 pip install redis
  2. from redis import *
  3. import time
  4. try:
  5. # 首先创建一个StrictRedis对象,便可以使用这个对象来操作redis数据库了
  6. # 参数 host:redis服务器的ip地址
  7. # port 6379
  8. # password 根据配置的需要 可选的操作
  9. # db:[0-15]
  10. redisObj=StrictRedis(host="192.168.136.134",port=6379,db=1,password='111111')
  11. print("连接redis服务器成功")
  12. redisObj.set('hobby','打篮球') 添加字符串类型的数据
  13. print(redisObj.get('hobby'))
  14. # 添加多个键值对
  15. redisObj.mset({'name1':'AA','name2':'BB','addr':'china.shenzhen'})
  16. rs=redisObj.mget('name1','name2','addr')
  17. print(rs)
  18. # 设定超时时间
  19. redisObj.setex('class',5,'three.class')
  20. print(redisObj.get('class'))
  21. time.sleep(7)
  22. print(redisObj.get('class'))
  23. print(redisObj.keys())
  24. redisObj.delete('name1','name2')
  25. print(redisObj.keys())
  26. # list 列表的使用
  27. redisObj.lpush('class','liyang','weo','fly')
  28. rs=redisObj.rpush('class', 'lifei88888')
  29. redisObj.lrem('class',1,'lifei88888') #删除一个数据
  30. redisObj.lset('class',3,555) #对索引对应的值进行修改
  31. result=redisObj.lrange('class',0,-1)
  32. print(redisObj.llen('class'))
  33. for index in range(3):
  34. print(redisObj.lpop('class')) #返回并删除键为 class 的列表中的首元素
  35. print(result)
  36. redisObj.lpush('AA', '1', 2)
  37. print(redisObj.keys())
  38. redisObj.delete('class')
  39. except Exception as ex:
  40. print(ex)