python连接MySQL,mongodb, redis的示例程序

9 篇文章 0 订阅
8 篇文章 0 订阅

MySQL

# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html

import pymysql


class ConnectMysql(object):
    """
    存储数据
    """

    def __init__(self):
        # 连接数据库
        self.connect = pymysql.connect(
            host='ip',
            db='dbname',
            user='root',
            passwd='pass',
            # charset='utf8',
            use_unicode=True)

        # 通过cursor执行增删查改
        self.cursor = self.connect.cursor()


    def _process_people(self, item):
        self.connect.ping(reconnect=True)
        


    def _process_relation(self, item):
        self.connect.ping(reconnect=True)
        

    def process_item(self, item, spider):
        """
        处理item
        """
        if isinstance(item, ZhihuPeopleItem):
            self._process_people(item)
        elif isinstance(item, ZhihuRelationItem):
            self._process_relation(item)
        return item

mongodb

import pymongo


class TencentPipeline(object):

    def __init__(self):
        self.connection = pymongo.MongoClient('ip', port, connect=False)
        self.connection.admin.authenticate('name', 'pass')
        db = self.connection['taobao']
        db.authenticate('name', 'pass')
        self.collection1 = db['item']

    def process_item(self, item):

        item = dict(item)
        self.collection1.insert_one(item)

redis

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 
# @Time : 2019/5/29 10:03 
# @Author : zhao.jia
# @Site :  
# @File : read_redis.py 
# @Software: PyCharm

import redis

'''
这种连接是连接一次就断了,耗资源.端口默认6379,就不用写
r = redis.Redis(host='127.0.0.1',port=6379,password='tianxuroot')
r.set('name','root')

print(r.get('name').decode('utf8'))
'''
'''
连接池:
当程序创建数据源实例时,系统会一次性创建多个数据库连接,并把这些数据库连接保存在连接池中,当程序需要进行数据库访问时,
无需重新新建数据库连接,而是从连接池中取出一个空闲的数据库连接
'''
pool = redis.ConnectionPool(host='127.0.0.1',password='helloworld')   #实现一个连接池

r = redis.Redis(connection_pool=pool)
r.set('foo','bar')
print(r.get('foo').decode('utf8'))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

裸睡的雨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值