zoukankan      html  css  js  c++  java
  • python连接mysql,redis类方法

    #coding:utf8
    
    import pymysql
    import redis
    
    import conf
    
    '''MYSQL DB Object'''
    class Mysql():
        def __init__(self, dict=""):
            parmas = {
                'host' : conf.MYSQL_HOST,
                'port' : conf.MYSQL_PORT,
                'user' : conf.MYSQL_USER,
                'password' : conf.MYSQL_PASS,
                'db' : conf.MYSQL_DB,
                'charset' : conf.MYSQL_CHARSET,
            }
            if dict:
                parmas["cursorclass"] = pymysql.cursors.DictCursor
    
            self.conn = pymysql.connect(**parmas)
            self.cursor = self.conn.cursor()
    
        def fetchone(self, query):
            self.cursor.execute(query)
            self.conn.commit()
            return self.cursor.fetchone()
    
        def fetchall(self, query):
            self.cursor.execute(query)
            self.conn.commit()
            return self.cursor.fetchall()
    
        def write(self, query):
            write_count = self.cursor.execute(query)
            self.conn.commit()
            return write_count
    
    
    '''Redis DB Object'''
    class Redis():
        def __init__(self):
            parmas = {
                'host' : conf.REDIS_HOST,
                'port' : conf.REDIS_PORT,
                'password' : conf.REDIS_PASS,
            }
            self.cursor = redis.Redis(**parmas)
  • 相关阅读:
    hdu 4864 Task
    hdu 1501 Zipper
    hdu 1428 漫步校园
    hdu 1505 City Game
    hdu 1337 The Drunk Jailer
    9-13记录
    python 读取unicode编码文件
    梯度出现Nan值的追踪
    Rstudio-server更改R版本
    stdout/stderr作用学习
  • 原文地址:https://www.cnblogs.com/dylan-wu/p/8072458.html
Copyright © 2011-2022 走看看